2016-01-12 46 views
0

所以我試圖讓這個小腳本工作的樂趣,只是不能得到它的工作, 我有一個隨機名稱分配他們的'隨機'電子郵件地址數組,我想檢查電子郵件地址是否有擴展名: '@ hotmail.com', '@ hotmail.ca',或 '@ yahoo.ca'。如果他們接着迴應類似'丹尼爾,你的電子郵件地址擴展名是$擴展名,你有資格' ,如果他們不只是說'凱拉,你的電子郵件地址是$擴展名,你沒有資格。返回數組中的字符串的一部分

我想添加一個foreach語句來解釋數組$ classRoom2中的每個人,我嘗試過使用strstr(),但它不能在foreach內工作,因爲它只能有一個字符串。

繼承人什麼我走到這一步:

qualify = "";  
$ClassRoom2 = array(
      'Daniel' => '[email protected]', 
      'Mike' => 'dkoko[email protected]', 
      'Meranda' => '[email protected]', 
      'Will' => '[email protected]', 
      'Brittey' => '[email protected]', 
      'Kayla' => '[email protected]'); 


    switch ($ClassRoom2) { 
     case '@hotmail.com': 
      echo 'You are using extension '. $final; $qualify = 1; break; 

     case '@hotmail.ca': 
      echo 'You are using extension '. $final; $qualify = 1; break; 

     case '@yahoo.com': 
      echo 'You are using extension '. $final; $quality = 1; break; 

     case '@yahoo.ca': 
      echo 'You are using extension '. $final; $qualify = 1; break; 

     case '@live.ca': 
      echo 'You are using extension '. $final; $quality = 1; break; 

     default: 
      echo 'its something else'; $qualify = 0; 
      break; 
    } 


    if ($qualify == 1) { 
     echo "Congratulations, you quality for the contest. The extension you chose was <b>$final</b>"; 
    } else { 
     echo "Sorry mate! you didn't quality for the contest."; 
    } 
+1

使用'爆炸( '@', '[email protected]')'.. – check

+0

'strrpos('[email protected]」 ,'@ hotmail.com',0)=== 0' –

+0

您可能想要使用[loop](http://php.net/manual/en/language.control-structures.php)和[regular表達](http://php.net/manual/en/book.pcre.php),例如'@hotmail \ [A-Z] {2,3}'。看看['foreach()'](http://php.net/manual/en/control-structures.foreach.php)和['preg_match()'](http://php.net/manual /en/function.preg-match.php) – Havelock

回答

0

對不起,我遲到的反應,我還在測試我的代碼示例。

如果你想堅持你目前設計的開關,你可以使用一個簡單的preg_match來提取你需要的字符串。這裏是一個小例子(你可以刪除註釋,並把你的交換機有):

<?php 
$ClassRoom2 = array( 
    'Daniel' => '[email protected]', 
    'Mike' => '[email protected]', 
    'Meranda' => '[email protected]', 
    'Will' => '[email protected]', 
    'Brittey' => '[email protected]', 
    'Kayla' => '[email protected]' 
); 

foreach ($ClassRoom2 as $name=>$email) { 
    $matches = []; 
    preg_match("/(@.+)/", $email, $matches); 

    // Do things with $matches[0] here (your switch for eg) 
    // switch ($matches[0]) { 
    // case '@hotmail.com': 
    // ... 

    print '<br/> ' . $matches[0]; 
} 
?> 

如果你願意,你可以撥弄以防萬一用預浸本網站相匹配:regexr

更新你其實可以做很多事preg_match,一旦你得到了它的竅門:)

foreach ($ClassRoom2 as $name=>$email) { 
    $matches = preg_match("/@(hotmail.com|hotmail.ca|yahoo.ca|yahoo.com|live.ca)/", $email); 
    // if ($matches) // or simply replace the preg_match in the if 
    print '<br/> ' . $email . ($matches ? ' qualifies' : ' <strong>does not qualify</strong> ') . 'for the email test'; 
} 
1

使用爆炸()來獲取域部分,並比較其

$parts = explode("@", "[email protected]"); 
echo ($parts[1]=='domain.com') ? 'qualify' : 'not qualify'; 
0

我把條目和單獨的數組資格的擴展名列表,然後檢查每個人的恩嘗試並解析他們的信息,看是否符合每一個,就像這樣:

$peoplelist = array(
    'Daniel' => '[email protected]', 
    'Mike' => '[email protected]', 
    'Meranda' => '[email protected]', 
    'Will' => '[email protected]', 
    'Brittey' => '[email protected]', 
    'Kayla' => '[email protected]' 
); 

$qualify = array(
    'hotmail.com', 
    'hotmail.ca', 
    'yahoo.com', 
    'yahoo.ca', 
    'live.ca', 
); 

foreach($peoplelist as $name => $email) 
{ 
    $parts = explode("@", $email); 
    $extension = strtolower(trim(array_pop($parts))); 

    echo "Hi, ".$name.". You are using extension @".$extension.". <br /> \n"; 

    if(in_array($extension, $qualify)) 
    { 
     echo "Congratulations, you quality for the contest. <br /> \n"; 
    } 
} 
相關問題