Php verifies the mobile phone number and verifies the mobile phone number _ PHP Tutorial

Source: Internet
Author: User
Php verifies the mobile phone number and verifies the mobile phone number. Php verifies the mobile phone number. it is common sense to verify the mobile phone number. Chinese mobile phone numbers start with the number "1" and then use "0 ~ 9 "10 digits composed of 11 digits combined with php to verify the mobile phone number, verify the mobile phone number

Everyone should have this knowledge. Chinese mobile phone numbers start with numbers "1", and then use "0 ~ 9 "the 11-digit combination composed of 10 digits, so our verification rules must be written according to this idea.
Based on the simple idea above, we can write the following verification code:

<? Php // this code only provides some ideas // There are still some distance between the actual application and $ mobile = '000000 '; // the following 1 is the first number of the mobile phone. it must be a number. // [0-9] indicates that the number following the phone is 0-9 ~ 9: // {9} the 9 in the middle refers to the number of the mobile phone number in addition to the first digit, the other number must be repeated 10 times, exactly 11 digits if (! Preg_match ('/^ 1 ([0-9] {9})/', $ mobile) exit ('your mobile phone number is incorrect ');?>

But the above code is obviously not rigorous enough, because in this way, even if the mobile phone number "18888888888" can pass verification, we need to perform more rigorous verification on it, before getting strict, let's take a look at the first three common mobile phone numbers in China:
Mobile phone number:134, 135, 136, 137, 138, 139, 150, 151, 157 (TD), 158, 159, 187, 188

China Unicom mobile phone number:130, 131, 132, 152, 155, 156, 185, 186

China Telecom mobile phone number:133, 153, 180, 189, (1349 Weitong)
Based on the above features, we can modify the code into 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 the function $ str = checkMobile ("15800000001"); if ($ str) {echo ("compliant with mobile phone number standards");} else {echo ("not compliant with mobile phone number standards");}?>

The above code can only start with. if there is a new segment, we can modify it. the code is as follows:

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

In this way, all the mobile phone numbers can be verified, and the entire parsing logic is Interlocking. The php verification mobile phone number is analyzed in detail to find a solution.

The above is the php mobile phone number verification method shared for everyone. I hope it will be helpful for everyone's learning.

Everyone should have this knowledge. Chinese mobile phone numbers start with numbers "1" and then use "0 ~ 9. an 11-digit combination of 10 digits...

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.