preg_match,无法获取电子邮件地址

时间:2012-12-19 11:57:53

标签: php preg-match

我需要从html代码

下面检索电子邮件数据
<a href="mailto:contact@rovio.com" rel="nofollow">Email Developer</a>

我已经使用此代码获取电子邮件地址,但未能获得它。

preg_match('@^(?:mailto:)?([^/]+)@i', $data, $matches);
$email = $matches[1];
echo $email;

你能看出问题是什么吗?

1 个答案:

答案 0 :(得分:0)

你能做的就是

  • 从href属性中删除mailto:
  • 然后过滤邮件地址

$string = '<a href="mailto:contact@rovio.com" rel="nofollow">Email Developer</a>';
$parsing = new SimpleXmlElement($string);
$attrs = (array)$parsing->attributes();
$mail = str_replace('mailto:','',$attrs['href']);
if(filter_var($mail,FILTER_VALIATE_MAIL)){//if it is a good email
   //goooo
}
相关问题