doufu6196 2015-11-23 23:26
浏览 80
已采纳

如何使用PHP中的电子邮件ID数组检查特定域的电子邮件ID?

I've an array of email ids. I want to check each and every email id for it's domain. Actually, I've to parse over this array whenever there is email id found with no '.edu' domain, error message should be thrown as 'Please enter valid .edu id' and further emails from the array should not be checked.

How should I achieve this in efficient and reliable way?

Following is my code of array which contains the email ids. The array could be empty, contain single element or multiple element. It should work for all of these scenarios with proper validation and error messages.

$aVals = $request_data;
$aVals['invite_emails'] = implode(', ', $aVals['invite_emails']);

$aVals['invite_emails'] contains the list of email ids received in request.

Please let me know if you need any further information regarding my requirement if it's not clear to you.

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dongpin3794 2015-11-23 23:50
    关注

    You can do something like this,

    Updated:

    // consider $aVals['invite_emails'] being your array of email ids
    // $aVals['invite_emails'] = array("rajdeep@mit.edu", "subhadeep@gmail.com");
    
    if(!empty($aVals['invite_emails'])){  //checks if the array is empty
        foreach($aVals['invite_emails'] as $email){  // loop through each email
            $domains = explode(".",explode("@",$email)[1]); // extract the top level domains from the email address
            if(!in_array("edu", $domains)){  // check if edu domain exists or not
                echo "Please enter valid .edu id";
                break;  // further emails from the array will not be checked
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部