您的位置:首页 > 编程语言 > PHP开发

PHP 匹配电话,手机,400号码 函数 及正则。很管用。

2012-12-21 18:35 666 查看
PHP 匹配电话格式是否正确,自己写的。

1,如果是电话,那么区号和电话之间必须有横杠,并且必须有区号

2,如果是手机,可以支持手机前带上“+86”、“86”、“86-”、“+86-”前缀

3,电话只能匹配中国大陆的。02开头共三位,01开头的只允许010北京的号

4,支持电话分机号码,020-89571800-1,020-89571800-125。

5,400电话支持

<?php

function isTel($tel,$type='')
{
$regxArr = array(
'sj'  =>  '/^(\+?86-?)?(18|15|13)[0-9]{9}$/',
'tel' =>  '/^(010|02\d{1}|0[3-9]\d{2})-\d{7,9}(-\d+)?$/',
'400' =>  '/^400(-\d{3,4}){2}$/',
);
if($type && isset($regxArr[$type]))
{
return preg_match($regxArr[$type], $tel) ? true:false;
}
foreach($regxArr as $regx)
{
if(preg_match($regx, $tel ))
{
return true;
}
}
return false;
}

$tm1 = microtime(true);
$arr = array(
'15910241024',
'+861591900 0000',
'86159 1930 9100',
'+86-15312001200',
'18655321002',

'02089571800',
'020-89571800',
'0755-102410240',
'0553-10241024',
'010-1204120140',
'010-120412014',
'110-89571800',
'022-102410240',
'0222-102410241',

'400-020-9800',
'400-0588-010',
'400-0211-0112',
);
foreach ($arr as $tel)
{
echo "\n{$tel}:\t".(isTel($tel) ? "是":"错误");
}
echo sprintf("\n\ntaken %.6f s",microtime(true)-$tm1);


测试结果:

X-Powered-By: PHP/5.2.0
Content-type: text/html

15910241024:	是
+861591900 0000:	错误
86159 1930 9100:	错误
+86-15312001200:	是
18655321002:	是
02089571800:	错误
020-89571800:	是
0755-102410240:	是
0553-10241024:	是
010-1204120140:	错误
010-120412014:	是
110-89571800:	错误
022-102410240:	是
0222-102410241:	错误
400-020-9800:	是
400-0588-010:	是
400-0211-0112:	是

taken 0.002274 s
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: