PHP爆炸无法正常使用逗号[关闭]

PHP爆炸无法正常使用逗号[关闭]

问题描述:

Why this code isn't working properly? The string should be divided by commas but it looks like it's divided by commas and spaces at the same time (or something like that).

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

It should be:

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

But it's:

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

Ah I fixed it already. The problem was in "<" formatting.

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

EDIT:

$email = htmlspecialchars($email);