从 PHP 字符串中去除电子邮件地址
我为一家律师事务所管理电子邮件档案,该事务所从 Postfix 接收邮件并使用 PHP 脚本将消息插入数据库。这大部分工作正常,但有时我用来解析 From、To 和 Cc 标头中的电子邮件地址的正则表达式不能 100% 准确地捕获电子邮件地址。我已经尝试了 stackoverflow 上提出的其他解决方案(使用 filter_var()、使用 imap_rfc822_parse_adrlist、使用问题 1028553 中的正则表达式),但实际上比我所取得的成功要少。
我希望最大限度地减少系统调用(我现在使用了太多的 pregs)并提高准确性。当前函数接受标题文本(发件人、收件人或抄送字段)的输入,并返回去掉括号、引号、注释等的“干净”电子邮件地址。
任何人都可以提供的帮助将不胜感激,就像我一样难住了!
温迪
我的职能:
function return_proper ($email_string) {
if (is_array($email_string)) {
$x = "";
foreach ($email_string as $val) {
$x .= "$val,";
}
$email_string = substr($x, 0, -1);
}
$email_string = strtolower(preg_replace('/.*?([A-Za-z0-9\_\+\.\'-]+@[A-Za-z0-9\.-]+).*?/', '$1,', $email_string));
$email_string = preg_replace('/\>/', "", $email_string);
$email_string = preg_replace('/,$/', "", $email_string);
$email_string = preg_replace('/^\'/', "", $email_string);
return $email_string;
}
I operate an archive of e-mail for a law firm that receives mail from Postfix and uses a PHP script to insert messages into a database. This works mostly fine but sometimes the regular expression I use to parse e-mail addresses from the From, To, and Cc headers does not capture e-mail addresses with 100% accuracy. I have tried the other solutions posited here on stackoverflow (using filter_var(), using imap_rfc822_parse_adrlist, using the regex in question 1028553) with actually less success than what I have.
I am looking to minimize system calls (I use way too many pregs right now) and increase accuracy. The current function takes an input of header text (the From, To, or Cc fields) and returns "clean" e-mail addresses stripped of brackets, quotes, comments, etc.
Any help anyone can provide would be appreciated, as I am stumped!
Wendy
My function:
function return_proper ($email_string) {
if (is_array($email_string)) {
$x = "";
foreach ($email_string as $val) {
$x .= "$val,";
}
$email_string = substr($x, 0, -1);
}
$email_string = strtolower(preg_replace('/.*?([A-Za-z0-9\_\+\.\'-]+@[A-Za-z0-9\.-]+).*?/', '$1,', $email_string));
$email_string = preg_replace('/\>/', "", $email_string);
$email_string = preg_replace('/,$/', "", $email_string);
$email_string = preg_replace('/^\'/', "", $email_string);
return $email_string;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)