Similar to this question, how could I parse e-mail addresses which are in this format,
"Bob Smith" <bob@company.com>, joe@company.com, "John Doe"<john@company.com>
And get the a result like this:
array(
'bob@company.com'=>'Bob Smith'
'joe@company.com'=>''
'john@company.com'=>'John Doe'
);
Well, you could use mailparse_rfc822_parse_addresses()
, which does exactly that. It's a PECL extension, so it might be easier to use Mail_RFC822::parseAddressList()
as mentioned in the comments.
This should work with just about anything:
$str = '"Bob Smith" <bob@company.com>, joe@company.com, "John Doe"<john@company.com>, Billy Doe<billy@company.com>';
$emails = array();
if(preg_match_all('/\s*"?([^><,"]+)"?\s*((?:<[^><,]+>)?)\s*/', $str, $matches, PREG_SET_ORDER) > 0)
{
foreach($matches as $m)
{
if(! empty($m[2]))
{
$emails[trim($m[2], '<>')] = $m[1];
}
else
{
$emails[$m[1]] = '';
}
}
}
print_r($emails);
Result:
Array
(
[bob@company.com] => Bob Smith
[joe@company.com] =>
[john@company.com] => John Doe
[billy@company.com] => Billy Doe
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With