PHP Verification of mobile phone number _php tips

Source: Internet
Author: User

Everyone should have this common sense, China's mobile phone number is the number "1", and then use the "0~9" 10 digits of the 11-digit combination, then our validation rules must be based on this idea to write.
Based on the above simple idea, we can write down the following verification code, the code is as follows:

<?php//  
  This code is only to provide some ideas  
  //distance to practical applications and some distance  
   $mobile = ' 15858588585 ';  
  The following 1 is the first digit of the mobile phone must be the number 1//[0-9] refers to the number of  
  0~9 after the number  
  //{9} The middle of the 9 refers to the mobile phone number, in addition to the first digit, the other to repeat 10 times, just is 11 digits of the  
  if (!preg_ Match ('/^1 [0-9]{9}]/', $mobile) "Exit (' your cell phone number is not correct ');  
? > 

But the above code is obviously not rigorous, because so, even if "18888888888" such a mobile phone number can be validated, so we have to do more rigorous verification, before strictly let us look at the common Chinese mobile phone number of the top three have those:
Mobile phone Number:134, 135, 136, 137, 138, 139, 150, 151, 157 (TD), 158, 159, 187, 188

Unicom Mobile phone Number:130, 131, 132, 152, 155, 156, 185, 186

Telecom Mobile phone Number:133, 153, 180, 189, (1349 health)
So, based on the characteristics above, we can change the code to such a rule, the code is as follows:

<?php 
   function Checkmobile ($str) 
   { 
     $pattern = "/^ (13|15) d{9}$/"; 
     if (Preg_match ($pattern, $str)) 
     {return 
       true; 
     } 
     else 
     {return 
       false; 
     } 
   } 
  Call function 
  $str = Checkmobile ("15800000001″); 
   " if ($str) 
   { 
   echo ("compliant with mobile phone number standard"); 
   } 
   else 
   { 
   echo ("does not conform to mobile phone number standard"); 
  > 

The above can only 骓 13, 15 start, if there is a new paragraph is not, we can modify the code as follows:

function Is_mobile ($str) {return Preg_match (/^ ((  
d{3}) | d{3}-))? 13d{9}$/", $str);  
 

This will be able to verify all mobile phone numbers, the whole analytic thinking is interlocking, for PHP verification mobile phone number for detailed analysis, to obtain solutions.

The above is for you to share the PHP verification of mobile phone number method, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.