【问题标题】:Retrieving e-mails from any string从任何字符串中检索电子邮件
【发布时间】:2013-12-11 22:35:10
【问题描述】:

我有一个字符串,我想检索该字符串中包含的所有电子邮件。

我可以使用此代码获取@domain.com 部分:

 preg_match_all('{@(([^ ]*)\.com)}', $string, $matches);
 print_r($matches[1]);

我想得到的是:

name@domain.com

我用它来使 PHP 工作: getting emails out of string - regex syntax + preg_match_all

【问题讨论】:

标签: php regex


【解决方案1】:

您必须使用完整的电子邮件正则表达式:

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$

你可以在这里找到这个正则表达式:http://www.regular-expressions.info/email.html

【讨论】:

  • 感谢您的快速回复。但是我的数组是空的,正则表达式代码:z
  • ^$ 添加到表达式会使其失败,因为这意味着该字符串应该只包含一个电子邮件地址。我想知道为什么这会受到如此多的赞成......
猜你喜欢
  • 2017-04-03
  • 2013-03-09
  • 2019-04-30
  • 1970-01-01
  • 2023-03-31
  • 2012-04-17
  • 2021-05-31
  • 2020-02-14
  • 2016-06-17
相关资源
最近更新 更多