PHP爆炸不能正常使用逗号

时间:2018-02-28 10:27:43

标签: php explode

为什么此代码无法正常运行?字符串应该用逗号分隔,但它看起来像是用逗号和空格同时划分(或类似的东西)。

// example $str
$str = 'Test Tset <test@test.com>, Foo Abc <foo@abc.com>, Another Email <anotheremail@mail.test>, email_wo_brackets@gmail.com';

$emails = [];
$email_list = explode(',', $str);

foreach ($email_list as $email) {
    if (!in_array($email, $emails) and strpos($email, '@') !== false) {
        $emails[] = $email;
        echo sizeof($emails) . " $email<br>";
    }
}

应该是:

1 Test Tset <test@test.com>
2 Foo Abc <foo@abc.com>
3 Another Email <anotheremail@mail.test>
4 email_wo_brackets@gmail.com

但它是:

1 Test Tset 
2 Foo Abc 
3 Another Email 
4 email_wo_brackets@gmail.com

1 个答案:

答案 0 :(得分:1)

啊我已经修好了。问题出在&#34;&lt;&#34;格式化。

$email = str_replace(['<', '>'], '%%', $email);

修改

$email = htmlspecialchars($email);