We will use regular expressions to match mobile phone numbers. Let’s first analyze the encoding rules of mobile phone numbers:

  1. Cell phone numbers are all 11-digit
  2. All mobile phone numbers start with 1
  3. The second number currently has 34578 these numbers, after will increase 1, 2, 6, 9, I can only say very likely, the program ape is helpless pain!

PHP uses regular expressions to determine the number of a mobile phone:

g=”/1[34578]\d9g = “/^1[34578]\d{9}g=”/1[34578]\d9/”; // the “^” symbol must start with 1; “[]” means that the second number must be a number in brackets; While \d denotes any number from 0 to 9, followed by {9} to indicate a length of 9 digits; The $after it indicates the end; Regular expressions that start and end with/must be placed in the middle, and some may be followed by patterns.

Example: 1. Extract all mobile phone numbers from a text

STR = ‘My cell phone number 13800001111, your cell phone number 18900009999, his cell phone number 17710101212’; Pregmatchall (“/1[34578]\d9str = ‘My cell phone number is 13800001111, your cell phone number is 18900009999, his cell phone number is 17710101212’; Preg_match_all (“/^1[34578]\d{9} STR = ‘My cell phone number is 13800001111, your cell phone number is 18900009999, his cell phone number is 17710101212’; pregmatchall(“/1[34578]\d9/”, str,str, str,mobiles); var_dump($mobiles);

2. Check if a string is a valid mobile phone number

If (preg_match (” / ^ 1 [34578] \ d {9} / “, / “/”, mobile)) {echo ‘match, it is mobile phone number; }