使用引号中的逗号分隔逗号分隔的字符串中的电子邮件地址
问题描述:
Say I have the following email addresses comma separated in a string:
$addrList = 'Kevin.Brine@pppg.com, Robert.Thomas@pppg.com, "Guice, Doug" <Doug.Guice@pppg.com>';
I need the following result:
[0] => Kevin.Brine@pppg.com
[1] => Robert.Thomas@pppg.com
[2] => "Guice, Doug" <Doug.Guice@pppg.com>
Seems fairly simple, but that comma in the quoted name really has thrown me in trying to find a solution using either preg_match_all or preg_split. Also we should accommodate for emails that use single quotes for names too, ie: 'smith, tom' <tom.smith@abc.com>
假设我在字符串中分隔了以下电子邮件地址: p>
$ addrList ='Kevin.Brine @ pppg.com,Robert.Thomas @ pppg.com,“Guice,Doug”&lt; Doug.Guice@pppg.com>';
code> pre>
我需要以下结果: p>
[0] =&gt; Kevin.Brine@pppg.com
[1] =&gt; Robert.Thomas@pppg.com
[2] =&gt; “Guice,Doug”&lt; Doug.Guice@pppg.com>
code> pre>
看起来相当简单,但引用名称中的逗号确实让我试图 使用preg_match_all或preg_split找到解决方案。 此外,我们也应该容纳使用单引号的电子邮件,即:'smith,tom'&lt; tom.smith@abc.com> code> p>
div>
答
str_getcsv() should give you what you need, though it will strip the quotes.
EDIT
If you want to put the quotes back in:
$addrList = 'Kevin.Brine@pppg.com, Robert.Thomas@pppg.com, "Guice, Doug" <Doug.Guice@pppg.com>';
$t = str_getcsv($addrList);
foreach($t as $k => $v) {
if (strpos($v,',') !== false) {
$t[$k] = '"'.str_replace(' <','" <',$v);
}
}
var_dump($t);