[英]PHP Email Array Regular Expression
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. PHP的Mailparse扩展具有您可能想尝试的
mailparse_rfc822_parse_addresses
函数 。 Otherwise you should build your own address parser. 否则,您应该构建自己的地址解析器。
You could use preg_match_all
( docs ): 您可以使用
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
. 假设所有地址都包含在
< ... >
,则无需explode()
字符串$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). 编辑#2将解析器用于任何非平凡的数据(请参阅下面的注释-电子邮件地址解析是一个bit子)。 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);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.