ID card is an important document. For websites, apps and other applications, it is often necessary to verify whether the ID number entered by the user meets the specifications. PHP provides a very convenient way to achieve this function, that is, using regular expressions.
This article will introduce how to use PHP regular expressions to verify the correct format of the ID number.
1. ID card number format
Before using PHP regular expressions to verify the ID card number format, we first need to understand the format regulations of the ID card number. China's ID card number format has a total of 18 digits, the first 17 digits are numbers, and the last digit is a check code (which may be numbers or capital letters).
The specific provisions are as follows:
Now that we know the format of the ID number, we can use PHP regular expressions to verify its correctness.
2. Regular expression verification
The preg_match function is usually used to verify regular expressions. The syntax is as follows:
preg_match($pattern, $subject);
Among them, $pattern represents the regular expression and $subject represents the need Verification string. If the match is successful, return 1, otherwise return 0.
Next, we need to build a regular expression that can verify the format of the ID number. The specific implementation method is as follows:
To sum up, the final regular expression can be obtained:
/^([1-9]d{5})(d{4})([0-9]{2})([0-9]{2})(d{3})(d{3})(d{2})(d[xXd])$/
Using PHP regular expressions to verify the ID number requires the preg_match function. The code example is as follows:
function check_id_card($id_card) { // 加上正则表达式 $reg = '/^([1-9]d{5})(d{4})([0-9]{2})([0-9]{2})(d{3})(d{3})(d{2})([dxX])$/'; if (preg_match($reg, $id_card, $matches)) { // 校验地址码 if (!check_address_code($matches[1])) { return false; } // 校验出生日期 if (!check_birthday($matches[2] . "-" . $matches[3] . "-" . $matches[4])) { return false; } // 校验顺序码 if (!check_order_code($matches[5])) { return false; } // 校验校验码 if (!check_verify_code($matches[0])) { return false; } return true; } return false; }
Among them, check_address_code, check_birthday, check_order_code, check_verify_code and other functions are used to verify the correctness of the address code, date of birth, sequence code and check code respectively, and need to be implemented according to the specific situation.
3. Summary
Through the above method, you can use PHP regular expressions to verify the correctness of the ID number. It should be noted that the verification of the ID number is not only a matter of format, but also needs to consider whether the address code is legal, whether the date of birth is within a reasonable range, whether the gender identifier of the sequence code complies with regulations, etc. Therefore, code implementation needs to take into account Many factors ensure the accuracy and reliability of verification results.
The above is the detailed content of How to verify ID card format using PHP regular expression. For more information, please follow other related articles on the PHP Chinese website!