Home >Backend Development >PHP Problem >How to verify mobile phone number in php
PHP mobile phone number verification method
Mobile phone numbers have become an indispensable contact method in people's daily lives, and the verification of mobile phone numbers is also a very important part. In scenarios such as website registration and SMS reception, in order to ensure the legality and accuracy of the mobile phone number, we need to use PHP language to verify whether the mobile phone number is legal. In this article, we will introduce in detail how to use PHP to verify mobile phone numbers from the following aspects.
In China, the mobile phone number is 11 digits, so we first need to verify whether the entered mobile phone number is 11 digits long, otherwise It's not a legitimate mobile phone number. In PHP, you can use the strlen() function to get the length of the string and determine whether the entered mobile phone number is 11 digits.
if (strlen($mobile) != 11) { // 不是一个合法的手机号码 }
In China, the first three digits of the mobile phone number represent different operators, so we need to verify the entered mobile phone number Verify the first three digits to ensure its legitimacy.
// 中国移动: 134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/198 // 中国联通: 130/131/132/155/156/185/186/166 // 中国电信: 133/149/153/173/177/180/181/189/199 $prefix = substr($mobile, 0, 3); // 截取前三位 if (!in_array($prefix, ['134', '135', '136', '137', '138', '139', '150', '151', '152', '157', '158', '159', '182', '183', '184', '187', '188', '198', '130', '131', '132', '155', '156', '185', '186', '166', '133', '149', '153', '173', '177', '180', '181', '189', '199'])) { // 不是一个合法的手机号码 }
In China, mobile phone numbers have certain format requirements. Common formats include the following:
// 11位数字,如:13912345678 // 带区号的11位数字,如:02012345678、075512345678 // 带“-”的11位数字,如:139-1234-5678、020-1234-5678、0755-1234-5678
Therefore, we need to use regular expressions to verify whether the above format requirements are met.
// 11位数字 if (!preg_match('/^1[3-9]\d{9}$/', $mobile)) { // 不是一个合法的手机号码 } // 带区号的11位数字 if (!preg_match('/^0\d{2,3}\d{7,8}$/', $mobile)) { // 不是一个合法的手机号码 } // 带“-”的11位数字 if (!preg_match('/^(\d{3}-|\d{4}-)?\d{7,8}$/', $mobile)) { // 不是一个合法的手机号码 }
Finally, we combine the above three verification methods to implement mobile phone number verification in PHP.
function validateMobile($mobile) { if (strlen($mobile) != 11) { return false; } $prefix = substr($mobile, 0, 3); if (!in_array($prefix, ['134', '135', '136', '137', '138', '139', '150', '151', '152', '157', '158', '159', '182', '183', '184', '187', '188', '198', '130', '131', '132', '155', '156', '185', '186', '166', '133', '149', '153', '173', '177', '180', '181', '189', '199'])) { return false; } if (!preg_match('/^1[3-9]\d{9}$/', $mobile) && !preg_match('/^0\d{2,3}\d{7,8}$/', $mobile) && !preg_match('/^(\d{3}-|\d{4}-)?\d{7,8}$/', $mobile)) { return false; } return true; } // 示例 if (validateMobile('13912345678')) { echo '手机号码合法'; } else { echo '手机号码不合法'; }
The above is the method to verify the mobile phone number in PHP. By combining the above three verification methods, you can effectively ensure the legality and accuracy of the entered mobile phone number. In actual development, this method can also be encapsulated into a public function to facilitate calling in multiple scenarios.
The above is the detailed content of How to verify mobile phone number in php. For more information, please follow other related articles on the PHP Chinese website!