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

php 手机号码正则表达试程序代码

WBOY
Release: 2016-06-13 11:34:09
Original
1091 people have browsed it

  代码如下

  function funcMtel($str)//手机号码正则表达试

  {

  return (preg_match("/(?:13d{1}|15[03689])d{8}$/",$str))?true:false;

  }

  测试

  18678785887

  返回为

  false

  原因分析,查看了发现上面正则只能验证以13,15开头的,自然18开头的是不可以用的

  修改后

  代码如下

  function funcMtel($str)//手机号码正则表达试

  {

  return (preg_match("/(?:1[3|4|5|8]d{1}|15[03689])d{8}$/",$str))?true:false;

  }

  测试

  18678785887

  返回为

  true

  这样就成功了,

  总结经验:

  像手机号这类都会不断更新号码段了,我们在写函数时把它写成公共函数,这样就可以解决这个问题了。

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
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!