When using regular matching mobile phone number, we first analyze the law of mobile phone number:
1. Mobile phone number is usually 11-bit
2. Often the beginning of 1
3. The second figure is usually 34578 of these figures, the mobile phone number for the 170th segment of 2014.5.5 is on sale so there is a 7 more.
PHP uses regular expressions to determine how a mobile phone number is written:
- $g = "/^1[34578]\d{9}$/";
The "^" symbol indicates that the 1 must be the beginning; "[]" means that the second digit must be a number in brackets; The \d represents 0-9 arbitrary numbers, followed by {9}, which indicates a length of 9 digits; The following $ represents the end; The start and end/regular expressions must be placed in the middle, and some may also follow the pattern.
This can be written by matching all the phone numbers in the article:
- $str = ' Old Bar blog author's cell phone is 18912345678 ';
- Preg_match_all ("/^1[34578]\d{9}$/", $str, $mobiles);
- Var_dump ($mobiles);
If you want to check whether the phone number entered by the user is correct, you can check this:
- if (Preg_match ("/^1[34578]\d{9}$/", $mobile)) {
- There's an infinite imagination here.
- }
PHP Regular method of judging mobile phone number