PHP根据手机号判断运营商(详细介绍附代码)

这篇文章主要介绍了PHP根据手机号判断运营商,详细介绍附代码,大家可以根据最新的号段进行添加即可,通过正则判断实现,需要的朋友可以参考下。

道理很简单,知道手机号规则 进行正则判断就可以

移动: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卫通)

HTML页面

  1. <!DOCTYPE html>
  2. <html >
  3. <head>
  4. <title>手机号归属</title>
  5. </head>
  6. <body>
  7. <input type="text" onblur="mobile_check($(this).val())" >
  8. </body>
  9. </html>
  10. <script type="text/javascript" src="__ROOT__/Public/admin/lib/jquery/1.9.1/jquery.min.js"></script>  //修改为自己的路径
  11. <script>
  12. /*
  13. 移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
  14. 联通:130、131、132、152、155、156、185、186
  15. 电信:133、153、180、189、(1349卫通)
  16. */
  17. var phone = '';
  18. function mobile_check(phone){
  19. if(phone.length !== 11){
  20. alert('未检测到正确的手机号码');
  21. return false;
  22. }
  23. $.ajax({
  24. url:"__CONTROLLER__/phone_check",
  25. async:false,
  26. dataType:'json',
  27. type:'post',
  28. data:{phone:phone},
  29. success:function(msg){
  30. alert(msg);
  31. }
  32. });
  33. }
  34. </script>

controller控制代码

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