Home > Article > Backend Development > How to verify provincial certificate number using PHP regular expression
With the development of the Internet, the leakage of personal information has become an issue that cannot be ignored. For example, the ID number is one of the important identity documents that we need to provide for many things. However, if the ID number is used maliciously or leaked, it will bring a lot of trouble to our lives. In order to strengthen the protection of personal information, we can write a program to verify whether the ID number entered by the user is legal. PHP regular expressions are a common method.
1. Composition rules of ID number
Before using regular expressions to verify ID numbers, let us first understand the composition rules of Chinese ID numbers:
1.18-digit identity The certificate number consists of 17 digits and 1 check code;
2. The first 6 digits are the administrative division code of the area;
3. The 7th to 14th digits are the date of birth, for example, 870905 means 1987 Born on September 5, 2018;
4. The 15th to 17th digits are the serial numbers, which represent the order in which the person registered in the local household registration management department;
5. The 18th digit is the check code, used to verify the ID card Correctness.
2. Use PHP regular expressions to verify ID number
In PHP, use the preg_match() function to match regular expressions. The following is a PHP code that uses regular expressions to verify ID numbers:
function validateIDCard($id_card) { //正则表达式 $reg = '/^d{17}[0-9x]$/i'; //匹配 if(preg_match($reg, $id_card)) { //校验码验证 $arr = str_split($id_card); $last = strtolower($arr[17]) == 'x' ? 10 : $arr[17]; $card_sum = 0; $wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); for($i = 0; $i < 17; $i++) { $card_sum += $arr[$i] * $wi[$i]; } $card_sum %= 11; $card_sum = 12 - $card_sum; if($card_sum == 10) { $card_sum = 'x'; } if($card_sum == strtolower($arr[17])) { return true; } } return false; }
In this code, we first define a regular expression $reg. In this regular expression, we use ^d {17}[0-9x]$ matches the 18-digit ID number, where d represents a number, {17} represents matching 17 digits, and [0-9x] represents that the last digit can be a number or the letter x.
Then we use the preg_match() function to perform regular matching. If the match is successful, the subsequent verification code verification will proceed. The check code verification is mainly by taking the product of each digit and multiplying it by a fixed factor, and finally verifying the result. If the check code is correct, it means that the ID number is correct.
3. Summary
The correctness of the ID number is very critical. If it is incorrect, it will bring a lot of trouble to our lives. By writing a program, we can verify whether the ID number entered by the user is correct. This article introduces how to use PHP regular expressions to verify ID numbers. I hope this article can help everyone.
The above is the detailed content of How to verify provincial certificate number using PHP regular expression. For more information, please follow other related articles on the PHP Chinese website!