Given a list of emails, formated:
"FirstName Last" <email@address.com>, "NewFirst NewLast" <email2@address.com>
How can I build this into a string array of Only email addresses (I don't need the names).
PHP's Mailparse extension has a mailparse_rfc822_parse_addresses
function you might want to try. Otherwise you should build your own address parser.
You could use preg_match_all
( docs ):
preg_match_all('/<([^>]+)>/', $s, $matches);
print_r($matches); // inspect the resulting array
Provided that all addresses are enclosed in < ... >
there is no need to explode()
the string $s
.
EDIT In response to comments, the regex could be rewritten as '/<([^@]+@[^>]+)>/'
. Not sure whether this is fail-safe, though :)
EDIT #2 Use a parser for any non-trivial data (see the comments below - email address parsing is a bitch). Some errors could, however, be prevented by removing duplicate addresses.
<?php
$s = "\"FirstName Last\" <email@address.com>, \"NewFirst NewLast\" <email2@address.com>";
$emails = array();
foreach (split(",", $s) as $full)
{
preg_match("/.*<([^>]+)/", $full, $email);
$emails[] = $email[1];
}
print_r($emails);
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.