2017-01-24 98 views
0

我有一個preg_math的問題。所以我有這個梅索德:只獲取郵件的指定名稱

public function filterUsers($a_users){ 

    $a_authorizedEmail = array(
     '@test.com', 
     '@test1.com' 
    ); 

    $a_response = array(); 
    foreach ($a_users as $user) { 
     if(false !== $email_user = EmailRepository::getEmail($user['id'])){ 
      foreach($a_authorizedEmail as $email){ 
       echo $email_user; 
       if(preg_match($email, $email_user)){ 
        $a_response[]= $user; 
       } 
      } 
     } 
    } 
    return $a_response; 
} 

的陣列$a_users:我有一個電子郵件[email protected]用戶。但在返回數組是空的。可能我沒有做出正確的驗證。請幫我

+0

你可以從一個正則表達式的教程中受益。 [這一個](https://regexone.com/references/php)看起來不錯。 – apokryfos

+0

不確定你想要什麼。但似乎您需要使用'str_pos'或'explode'來獲取電子郵件主機 – aperpen

回答

2

這樣做:

public function filterUsers($a_users){ 

    $a_authorizedEmail = array(
     '@test.com', 
     '@test1.com' 
    ); 
    $pattern = "/".implode("|",$a_authorizedEmail)."$/"; //Note the "/" start and end delimiter as well as the $ that indicates end of line 
    $a_response = array(); 
    foreach ($a_users as $user) { 
     $email_user = EmailRepository::getEmail($user['id']); 
     if(false !== $email_user){ 
      echo $email_user; 
      if(preg_match($pattern, $email_user)){ 
       $a_response[]= $user; 
      } 
     } 
    } 
    return $a_response; 
}