开发者

Splitting comma separated email addresses in a string with commas in quotes

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>


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜