2015-11-24 57 views
1

我有一個電子郵件ID的數組。我想檢查每個電子郵件ID是否爲域名。 其實,我已經在這個陣列來解析每當有沒有「.edu的」域,錯誤信息應該被拋出‘請輸入有效的.edu的ID’進一步電子郵件從陣列中不應該是找到電子郵件ID檢查。如何使用PHP中電子郵件ID數組中的特定域來檢查電子郵件ID?

我應該如何在高效和可靠的方式實現這一目標?

以下是我其中包含電子郵件ID陣列的代碼。該數組可以是空的,包含單個元素或多個元素。它應該適用於所有這些場景,並具有適當的驗證和錯誤消息。

$aVals['invite_emails']包含請求中收到的電子郵件ID列表。

請讓我知道如果您需要更多關於我的要求任何進一步的信息,如果它不是你清楚。

在此先感謝。

+0

我想這個鏈接將解決您的問題: http://stackoverflow.com/questions/20255150/domain-specific-email-validation – Nehal

+0

[檢驗T他(https://eval.in/474917) –

回答

2

你可以做這樣的事情,

更新時間:

// consider $aVals['invite_emails'] being your array of email ids 
// $aVals['invite_emails'] = array("[email protected]", "[email protected]"); 

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 
     } 
    } 
} 
2

隨着電子郵件ID總是由3個字符組成的,你也可以做這樣的事情:

foreach($aVals['invite_emails'] as $email){ 
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { 
    // valid email 
      if(substr($email, -3) != "edu") { 
       echo "Please enter valid .edu id"; 
      } 
    } 
} 
相關問題