1

我有以下字符串:

“约翰尼测试” <johnny@test.com>,杰克<another@test.com>,“斯科特萨默斯” <scotts@test.com> ...

多词名称用双引号括起来

我需要一个包含以下结果的数组:

array(   
   array('nom' => 'Johnny Test', 'adresse' => 'johnny@test.com'),
   array('nom' => 'Jack', 'adresse' => 'another@test.com'),     
   array('nom' => 'Scott Summers', 'adresse' => 'scotts@test.com') 
   ... 
   )
4

2 回答 2

0
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&lt;(.*?)&gt;,?/', $string, $hits);
print_r($hits);
于 2012-11-21T10:53:54.543 回答
0
$all  = array();
$data = '"Johnny Test" <johnny@test.com>,Jack <another@test.com>,"Scott Summers" <scotts@test.com>';
$emails = explode(',', $data);
foreach ($emails as $email)
{
    if (preg_match('/(.*) <(.*)>/', $email, $regs)) {
        $all[] = array(
            'nom'     => trim($regs[1], '"'), 
            'adresse' => $regs[2],
        );
    }
}

print_r($all);
于 2012-11-21T11:09:27.447 回答