php完美匹配邮箱、链接地址和电话号码

来源:互联网 发布:数据挖掘导论下载 编辑:程序博客网 时间:2024/05/16 05:27

2012/01/31日 更新:

匹配手机以及电话号码 重新修改,可支持18开头的手机号,并修改bug,可匹配出字符串中所有匹配号码!

经过多次测试,以下代码可以实现邮箱、链接地址和电话号码的匹配

 

匹配全部url地址和链接内容

====================

$str = "<a href='http://www.baidu.com' target='_blank'>百度</a>http://www<a href='http://www.sina.com' target='_blank'>新浪</a>kod";

preg_match_all("/<a href=([\"\'])(http:\/\/([\w\d\.])+)[^>]*>(.*?)<\/a>/i", $str, $matches);

echo "<pre>";
var_dump($matches[2]);
echo "<br />";
var_dump($matches[4]);

 

匹配邮箱

====================

$emails = "wangzhanu@126.comha12wangzhanu@163.com";

preg_match_all("/([a-z0-9_\-\.]+)@(([a-z0-9]+[_\-]?)\.)+[a-z]{2,3}/i", $emails, $matches);

var_dump($matches[0]);

 

匹配手机以及电话号码

====================

$tel = "13911112222sf010-44444442dfg18811112222";

preg_match_all("/1[3,5,8]{1}[0-9]{1}[0-9]{8}|0[0-9]{2,3}-[0-9]{7,8}(-[0-9]{1,4})?/", $tel, $matches);

var_dump($matches);

 

请放心测试,有问题请大家留言

 

原创粉丝点击