Similar to this question, how could I parse e-mail addresses which are in this format,
"Bob Smith" <[email protected]>, [email protected], "John Doe"<[email protected]>
And get the a result like this:
array(
'[email protected]'=>'Bob Smith'
'[email protected]'=>''
'[email protected]'=>'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" <[email protected]>, [email protected], "John Doe"<[email protected]>, Billy Doe<[email protected]>';
$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
(
[[email protected]] => Bob Smith
[[email protected]] =>
[[email protected]] => John Doe
[[email protected]] => 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