preg_match_all('/(.*?)\s<(.*?)>,?/', $string, $hits);
print_r($hits);
像这样的东西应该工作。
如果您\r\n
在字符串中使用它,请在使用正则表达式解析它之前使用它:
$chars=array("\r\n", "\n", "\r");
$string=str_replace($chars, '', $string);
更新:我用来测试的代码。
test_preg2.php:
<?php
$html='"Johnny Test" <johnny@test.com>,Jack <another@test.com>,"Scott Summers" <scotts@test.com>';
$chars=array("\r\n", "\n", "\r");
$html=str_replace($chars, '', $html);
preg_match_all('/(.*?)\s<(.*?)>,?/', $html,$hits);
print_r($hits);
?>
输出:
Array ( [0] => Array ( [0] => "Johnny Test" , [1] => Jack , [2] => "Scott Summers" ) [1] => Array ( [0] => "Johnny Test" [1] => Jack [2] => "Scott Summers" ) [2] => Array ( [0] => johnny@test.com [1] => another@test.com [2] => scotts@test.com ) )
更新 2:字符串已使用 htmlentities() 格式化。(问题的示例字符串是错误的人......)
preg_match_all('/(.*?)\s<(.*?)>,?/', $string, $hits);
print_r($hits);