How to use PHP to determine the operator based on the mobile phone number (example)

墨辰丷
Release: 2023-03-26 07:18:01
Original
1763 people have browsed it

本篇文章主要介绍PHP如何实现根据手机号判断运营商,感兴趣的朋友参考下,希望对大家有所帮助。

HTML页面

<!DOCTYPE html>
<html lang="en">
<head>
  <title>手机号归属</title>
</head>
<body>
  <input type="text" onblur="mobile_check($(this).val())" >
</body>
</html>
<script type="text/javascript" src="__ROOT__/Public/admin/lib/jquery/1.9.1/jquery.min.js"></script>  //修改为自己的路径
<script>
  /*
   移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
   联通:130、131、132、152、155、156、185、186
   电信:133、153、180、189、(1349卫通)
   */
  var phone = &#39;&#39;;
  function mobile_check(phone){
    if(phone.length !== 11){
      alert(&#39;未检测到正确的手机号码&#39;);
      return false;
    }
    $.ajax({
      url:"__CONTROLLER__/phone_check",
      async:false,
      dataType:&#39;json&#39;,
      type:&#39;post&#39;,
      data:{phone:phone},
      success:function(msg){
        alert(msg);
      }
    });
  }
</script>
Copy after login

controller控制代码

/*
  *@param string $phone  手机号字符串
  *@return 0中国移动,1中国联通 2中国电信 3未知
  */
  public function phone_check(){
    if(IS_POST){
      $phone = I(&#39;phone&#39;);
      $isChinaMobile = "/^134[0-8]\d{7}$|^(?:13[5-9]|147|15[0-27-9]|178|18[2-478])\d{8}$/"; //移动方面最新答复
      $isChinaUnion = "/^(?:13[0-2]|145|15[56]|176|18[56])\d{8}$/"; //向联通微博确认并未回复
      $isChinaTelcom = "/^(?:133|153|177|173|18[019])\d{8}$/"; //1349号段 电信方面没给出答复,视作不存在
      // $isOtherTelphone = "/^170([059])\\d{7}$/";//其他运营商
      if(preg_match($isChinaMobile, $phone)){
        $this->ajaxReturn(&#39;中国移动&#39;); //0
      }else if(preg_match($isChinaUnion, $phone)){
        $this->ajaxReturn(&#39;中国联通&#39;); //1
      }else if(preg_match($isChinaTelcom, $phone)){
        $this->ajaxReturn(&#39;中国电信&#39;); //2
      }else{
        $this->ajaxReturn(&#39;未知&#39;);   //3
      }
    }
 
    $this->display();
  }
Copy after login

相关推荐:

如何判断手机号的所属通信商

PHP 判断手机号归属地

php 验证手机号码 判断手机号码归属地方

The above is the detailed content of How to use PHP to determine the operator based on the mobile phone number (example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template