Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-06 19:49:44
Original
1241 people have browsed it

PHP 匹配电话式是否正确,自己写的。 1,如果是电话,那么区号和电话之间必须有横杠,并且必须有区号 2,如果是手机,可以支持手机前带上“86”、“86”、“86-”、“86-”前缀 3,电话只能匹配中国大陆的。02开头共三位,01开头的只允许010北京的号 4,支持

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);
Copy after login





测试结果:


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
Copy after login




source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template