How to verify ID card number using PHP regular expression

PHPz
Release: 2023-06-24 16:38:02
Original
2213 people have browsed it

PHP, as a very popular programming language, provides powerful regular expression functions that can be used to verify various data formats input by users. Among them, the verification of ID number is a very common requirement, because the ID number is the only identity identifier and requires strict verification in many application scenarios. This article will introduce how to use PHP regular expressions to verify ID numbers.

1. Obtain the ID number

Before starting to verify the ID number, we need to obtain the ID number entered by the user. It can be obtained through form submission, API parameter transmission, etc., which will not be described here. Assume that the ID number we obtained is $id_number.

2. Rules for ID Numbers

Before performing regular expression verification, we need to first understand the rules for ID numbers. The ID card number is composed of 18 digits and letters. The first 17 digits include province, city, county (district) and other information, and the last digit is a check code. The specific rules are as follows:

1. The first 1 and 2 digits represent: the administrative division code of the province;

2. The 3rd and 4 digits represent: the administrative division code of the city;

3. The 5th and 6th digits represent: the administrative division code of the district (county);

4. The 7th to 14th digits represent: date of birth, for example, 701230 represents December 1970 March 30th;

5. The 15th to 17th digits represent: the code of the local police station;

6. The 18th digit is the check code, calculated from the first 17 digits.

According to the above rules, we can get the regular expression of the ID number:

/^[1-9]d{5}(19|20)d{2}((( 0[1-9])|(1[0-2]))((0[1-9])|([1-2]d)|(3[0-1]))d{3}[ 0-9xX]$/

Let’s analyze this regular expression one by one.

First of all, ^ represents the starting position of the matching string, and $ represents the ending position of the matching string. This ensures that the entire string conforms to the rules of the ID number.

Next, [1-9]d{5} represents the first 6 digits of the ID number, which can be any number from 1 to 9, followed by 5 digits. This is actually the administrative division code of a province, city, or county (district).

Then, (19|20)d{2} represents the first 8~9 digits of the ID number, which can be 19 or 20, followed by 2 digits. This indicates the year of birth.

Next, ((0[1-9])|(1[0-2])) represents the first 10~11 digits of the ID number, which can be any number from 01~12. This means Month of birth.

Then, ((0[1-9])|([1-2]d)|(3[0-1])) represents the first 12~13 digits of the ID number, which can be Any number from 01 to 31, this represents the date of birth.

Next, d{3} represents the first 14~16 digits of the ID number, which is the code of the local police station, and can be any 3 digits.

Finally, [0-9xX] represents the last digit of the ID number, which can be 0~9 or x or X. This is the check code.

3. Use of PHP regular expressions

After understanding the rules and regular expressions of ID numbers, we can use PHP’s preg_match function to verify the ID number entered by the user. . The specific usage is as follows:

$pattern = '/^[1-9]d{5}(19|20)d{2}((0[1-9])|(1[0-2]))((0[1-9])|([1-2]d)|(3[0-1]))d{3}[0-9xX]$/';
if(preg_match($pattern, $id_number)) {
    echo '身份证号码合法';
} else {
    echo '身份证号码不合法';
}
Copy after login

In the above code, $pattern is the regular expression we just defined, and $id_number is the ID number entered by the user. The preg_match function will verify the ID number entered by the user through regular expressions and return the matching result. If the match is successful, "ID card number is legal" is output, otherwise "ID card number is illegal" is output.

4. Summary

Through this article, we learned how to use PHP regular expressions to verify ID numbers. Although the rules for ID numbers are relatively complicated, verification can be easily implemented as long as you master the basic syntax of regular expressions and the rules for ID numbers. In practical applications, we can also customize our own regular expressions as needed to meet different verification needs.

The above is the detailed content of How to verify ID card number using PHP regular expression. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template