PHP Verified Mobile phone number

Source: Internet
Author: User

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

<?PHP//This code is just to provide some ideas//There are some distances from the actual application$mobile ='15858588585'; //The following 1 is the first digit of the phone must be the number 1//[0-9] refers to the number of the following numbers are 0~9//{9} The middle of 9 refers to the mobile phone number in addition to the first digit, the other to repeat 10 times, just 11 digits.  if(!preg_match ('/^1 ([0-9]{9})/', $mobile)) exit ('your phone number is not correct'); ?>

But the above code is obviously not rigorous enough, because this way, even if the "18888888888" mobile phone number can be verified, so we have to be more rigorous verification, before strict please let us first look at China's common 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

Mobile phone Number:133, 153, 180, 189, (1349 Wei Tong)
So according to the above features, we can change the code into such a rule, the code is as follows:

<?php function Checkmobile ($str) {$pattern= "/^ ( -| the) d{9}$/"; if(Preg_match ($pattern, $str)) {Returntrue; }      Else{Returnfalse; }    }   //calling Functions$str = Checkmobile ("15800000001″); if($STR) {echo ("compliant with mobile phone number standard"); }    Else{echo ("does not conform to mobile phone number standard"); }   ?>

Above can only Zhui 13, 15, if there is a new paragraph is not, we can modify, the code is as follows:

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

This will be able to verify all the mobile phone number, the entire analytic thinking closely linked to PHP verification mobile phone number for detailed analysis, to obtain a solution.

PHP Verified Mobile phone number

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.