如何在PHP中或使用正则表达式拆分名称和电子邮件地址

Kur*_*lmi 0 php regex

我有以下字符串:

"Test, User" < test@test.com >, "Another, Test" < another@test.com >, .........
Run Code Online (Sandbox Code Playgroud)

我想要以下结果:

array(
  array('name' => 'Test, User', 'email' => 'test@test.com'),
  array('name' => 'Another, Test', 'email' => 'another@test.com'),  
  ...........
) 
Run Code Online (Sandbox Code Playgroud)

cle*_*tus 9

preg_match_all() 似乎合适:

$in = '"Test, User" < test@test.com >, "Another, Test" < another@test.com >, .........';
preg_match_all('!"(.*?)"\s+<\s*(.*?)\s*>!', $in, $matches);
$out = array();
for ($i=0; $i<count($matches[0]); $i++) {
  $out[] = array(
    'name' => $matches[1][$i],
    'email' => $matches[2][$i],
  );
}
print_r($out);
Run Code Online (Sandbox Code Playgroud)

输出:

Array
(
    [0] => Array
        (
            [name] => Test, User
            [email] => test@test.com
        )

    [1] => Array
        (
            [name] => Another, Test
            [email] => another@test.com
        )

)
Run Code Online (Sandbox Code Playgroud)