Home > Backend Development > PHP Tutorial > How to verify the area code of the ID card number using PHP regular expression

How to verify the area code of the ID card number using PHP regular expression

王林
Release: 2023-06-24 11:42:02
Original
1148 people have browsed it

The ID number is the unique identifier of a citizen’s identity document, and the area code is an important part of the ID number. In PHP, we can use regular expressions to verify whether the area code in the ID number is correct. The following will introduce a method to verify the area code of the ID card number.

First of all, we need to understand the encoding rules of the area code in the ID number. The first six digits in the ID number represent the area code, the first two digits represent the province (autonomous region, municipality) code, the third digit represents the city (region) code, and the last three digits represent the county (district) code. Among them, each province and city has a corresponding coding list, which we can obtain by querying relevant information.

Before verifying the ID number area code, you need to save the area code list of the corresponding province and city in an array, as shown below:

$region_codes = array(
    '110000', '120000', '130000', '140000', '150000', '210000', '220000', '230000', '310000', '320000',
    '330000', '340000', '350000', '360000', '370000', '410000', '420000', '430000', '440000', '450000',
    '460000', '500000', '510000', '520000', '530000', '540000', '610000', '620000', '630000', '640000',
    '650000', '710000', '810000', '820000'
);
Copy after login

Then, we can use regular expressions to match whether the area code in the ID number is legal. This regular expression can be defined as:

$pattern = '/^[1-9][0-9]{5}$/';
Copy after login

The meaning of this regular expression is: a 6-digit number starting with non-zero. Among them, the first digit cannot be 0, and the following 5 digits can be any number.

Next, we can write a function to verify the area code of the ID number, as shown below:

function validateRegionCode($region_code) {
    global $region_codes;
    if (in_array($region_code, $region_codes)) {
        return true;
    } else {
        return false;
    }
}
Copy after login

This function accepts a parameter $region_code, which is used to represent the area code in the ID number. . A global variable $region_codes is used in the function, which represents the defined list of region codes. This function uses the in_array function to determine whether $region_code is in the $region_codes array. If so, it means that the region code is legal and returns true; otherwise, it means that the region code is illegal and returns false.

Finally, we can use this function to verify whether the area code in the ID number is legal, as shown below:

$region_code = substr($id_card, 0, 6); // 截取身份证号码中的地区码
if (validateRegionCode($region_code)) {
    echo '该身份证号码的地区码合法!';
} else {
    echo '该身份证号码的地区码不合法!';
}
Copy after login

In the above code, we first intercept the ID number through the substr function in the region code, and then call the validateRegionCode function to verify whether the region code is legal. If the area code is legal, the corresponding prompt message will be output.

In summary, through the method introduced above, we can use PHP regular expressions to verify whether the area code in the ID number is legal. This method is simple and practical, and can effectively improve the efficiency and accuracy of ID number verification.

The above is the detailed content of How to verify the area code of the 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