doufu6196 2015-11-24 07: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-24 07: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条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值