The previous article introduced you to " Let's talk about the regular expression function in PHP? How to use it (with code) 》, this article continues to introduce to you how we can customize the regular expression that matches mobile phone numbers in PHP?
How do we customize the regular expression that matches mobile phone numbers in PHP?
As for mobile phone numbers, we all know that in mainland China, they usually start with 1, and the middle two digits may be 3 4 5 7 8; if the second digit is 3, the third digit It may be 0-9. If the second digit is 4, the third digit may be 7.
If the second digit is 5, the third digit may be 0-8. If the second digit is 7. The third digit is 0-8. If the second digit is 8, the third digit may be 0-9. The next 8 digits can be any combination. When we get the mobile phone number, we need to perform regular matching and enter ( $pattre), for mobile phone numbers, we need exact matching, so we need to add (^$), we start with 1 and end with \d{8}. At this time, we need to add brackets and modify them. If we say The third digit is 3, then our third digit can be 0-9, which is our first case
Next, we enter a field and then match;
Code demonstration:
1 2 3 4 5 6 7 8 9 10 11 |
|
The demonstration results are as follows:
The demonstration results show that our matching result is 0, obviously we did not match successfully;
If I change the input field to 133..., we run it again and find that the matching result is 1;
1 |
|
The demonstration results are as follows:
By analogy, as long as we do not exceed the range of 0-9, the matching result can be 1;
If our second number is 4, then our third number can only be 7, so When we need to change the code to 47, the code is as follows:
1 |
|
When we change 131 to 141, we will find that the matching result is 0 (the code displays the result as follows), because we have declared that if our second number is If 4, then our third number can only be 7. When our output is 147, we will find that the matching result is 1;
##The code is as follows:1 2 |
|
1 2 |
|
1 2 |
|
1 2 |
|
PHP Video Tutorial"
The above is the detailed content of How do we customize the regular expression for matching mobile phone numbers in PHP? (with code). For more information, please follow other related articles on the PHP Chinese website!