Home  >  Article  >  Backend Development  >  PHP regular expression to verify the format of ID number

PHP regular expression to verify the format of ID number

王林
王林Original
2023-06-24 11:20:081792browse

ID card number is a very important identification document in our lives. It is also necessary information that we often need to fill in in various business transactions. Therefore, when developing web applications, it is very important to verify the correctness of the ID number.

In PHP, we can verify the format of the ID number through regular expressions. Below we will introduce in detail how to use regular expressions to verify the format of the ID number.

1. ID card number format

The ID number is composed of 18 digits and the letter X. The first 17 digits are the region code and date of birth code of the ID card, and the last digit is the verification code of the ID card.

The format of the ID number is as follows:

第1-6位是身份证的地区编码,由国家统计局制定;
第7-14位是身份证的出生日期,格式为YYYYMMDD;
第15-17位是身份证的顺序码,表示在当天出生的第几个人;
第18位是身份证的校验码,用于校验身份证号码的合法性。

2. Use regular expressions to verify the format of the ID number

Use regular expressions to verify the format of the ID number , you need to understand the basic syntax of regular expressions.

In PHP, we use the preg_match function to implement regular expression matching. This function accepts two parameters, the first parameter is the regular expression to be matched, and the second parameter is the string to be matched. If the match is successful, the function will return 1, otherwise it will return 0.

The regular expression for verifying the ID number is as follows:

/^(d{6})(19|20)(d{2})([01]d)([0123]d)(d{3})(d|X)$/i

The meaning of the regular expression is as follows:

/^:表示正则表达式的起始位置。
(d{6}):表示前6位数字是身份证的地区编码。
(19|20):表示身份证的出生年份必须是19或20开头。
(d{2}):表示身份证的出生月份,为两位数字。
([01]d):表示身份证的出生日,为01-31之间的数字。由于月份和天数可能不是两位数,因此需要使用[01]d的形式,只要保证第一位是0或1就行了。
([0123]d):表示身份证的出生年份,为01-12之间的数字。
(d{3}):表示身份证的顺序码,由三位数字组成。
(d|X):表示身份证的校验码,只能是数字或字母X。
$/i:表示正则表达式的结束位置。

The following is a PHP code example:

function check_id_card($id_card){
    $preg = "/^(d{6})(19|20)(d{2})([01]d)([0123]d)(d{3})(d|X)$/i";
    if(preg_match($preg, $id_card)){
        return true;
    }else{
        return false;
    }
}

This function accepts a parameter $id_card, which represents the ID number to be verified. It uses the preg_match function and the above regular expression to perform matching. If the match is successful, the function returns true, otherwise it returns false.

The above is how to use regular expressions to verify the format of the ID number. When developing web applications, we can encapsulate this function in a public function library to facilitate calling it on multiple pages. This can improve development efficiency and ensure the security of web applications.

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

Statement:
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