We used php regular expression to verify mobile phone numbers. The rule for mobile phone numbers is to have 11 characters and start with "13, 15, 18, in this case, we only need to classify and plan and implement the perfect mobile phone number verification regular expression.
Everyone should have this knowledge. Chinese mobile phone numbers start with numbers "1", and then use "0 ~ 9 "11 digits consisting of 10 digits
Number combination, then our verification rules must be written according to this idea.
Based on the simple idea above, we can write the following verification code:
The Code is as follows: |
Copy code |
<? Php // This Code only provides some ideas // There is still some distance from the actual application $ Mobile = '000000 '; // The following 1 indicates that the first number of the mobile phone must be 1. // [0-9] indicates that the numbers following them are all 0-9 ~ 9 digits // {9} The 9 in the middle indicates that the mobile phone number must be repeated 10 times except the first digit, Which is 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, because
We also need to perform more rigorous verification on the mobile phone number. Before the strict verification, please let us first see that the first three common mobile phone numbers in China all have
Some:
Mobile phone number: 134, 135, 136, 137, 138, 139, 150, 151, 157, 158 (TD), 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)
Then, based on the above features, we can modify the code into such a rule:
The Code is as follows: |
Copy code |
<? Php Function checkMobile ($ str) { $ Pattern = "/^ (13 | 15) d {9} $ /"; If (preg_match ($ pattern, $ str )) { Return true; } Else { Return false; } } // Call a function $ Str = checkMobile (15800000001 ″); If ($ str) { Echo ("compliant with mobile phone number standards "); } Else { Echo ("not compliant with mobile phone number Standards "); } ?> |
It can only start with. If a new segment does not work, we can modify it.
The Code is as follows: |
Copy code |
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.