返回数组中字符串的一部分

时间:2016-01-12 07:18:36

标签: php arrays foreach

所以我试图让这个小脚本起作用而且无法让它工作, 我有一系列随机名称,分配了他们的“随机”电子邮件地址,我想检查电子邮件地址是否有扩展名:  '@ hotmail.com',  '@ hotmail.ca',或  '@ yahoo.ca'。如果他们回复像'丹尼尔,你的电子邮件地址扩展是$扩展,你有资格' 如果他们不只是说'Kayla,您的电子邮件地址是$ extension,则不符合条件。

我想添加一个foreach语句,解释为数组$ ClassRoom2中的每个人,我尝试过使用strstr()但它在foreach中不起作用,因为它只能有一个字符串。

到目前为止我得到了什么:

qualify = "";       
 $ClassRoom2 =  array(
            'Daniel' => 'fresco@hotmail.com',
            'Mike' => 'dkoko@yahoo.ca',
            'Meranda' => 'brunnn_23@hotmail.ca',
            'Will' => 'yumyum03@wp.pl',
            'Brittey' => 's0sd@outlook.com',
            'Kayla' => 'hocklife@freebie.com' );


    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.";
    }

3 个答案:

答案 0 :(得分:1)

使用explode()获取域部分并进行比较

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

答案 1 :(得分:0)

对不起我后来的反应,我还在测试我的代码示例。

如果您想坚持使用当前设计的开关,可以使用简单的preg_match来提取所需的字符串。这是一个小例子(你可以删除评论并在那里放入你的开关):

<?php
$ClassRoom2 =  array(   
    'Daniel' => 'fresco@hotmail.com',
    'Mike' => 'dkoko@yahoo.ca',
    'Meranda' => 'brunnn_23@hotmail.ca',
    'Will' => 'yumyum03@wp.pl',
    'Brittey' => 's0sd@outlook.com',
    'Kayla' => 'hocklife@freebie.com' 
);

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];
}
?>

如果你愿意,你可以在这个网站上使用preg匹配进行调整: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';
}

答案 2 :(得分:0)

我会将条目列表和符合条件的扩展名放在不同的数组上,然后检查每个人的条目并解析他们的信息,看看每个人是否符合条件,如下所示:

$peoplelist =  array(
    'Daniel' => 'fresco@hotmail.com',
    'Mike' => 'dkoko@yahoo.ca',
    'Meranda' => 'brunnn_23@hotmail.ca',
    'Will' => 'yumyum03@wp.pl',
    'Brittey' => 's0sd@outlook.com',
    'Kayla' => 'hocklife@freebie.com' 
);

$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";
    }
}