Home >Backend Development >PHP Tutorial >How to verify the gender of an ID number using PHP regular expressions

How to verify the gender of an ID number using PHP regular expressions

WBOY
WBOYOriginal
2023-06-24 11:52:041927browse

In common ID numbers, the 17th digit of the number is called the "gender code" and is used to indicate the gender of the ID card holder. When verifying the validity of an ID number, we usually need to use regular expressions to make judgments and confirm the gender of the holder based on the value of the gender code. This article will introduce how to use PHP regular expressions to verify the gender of an ID number.

1. Format of ID number

The ID number is a unique identifier composed of 18 digits and letters, which contains the basic information and place of origin information of the person. The specific format is as follows:

6-digit area code (province, city, and county), 8-digit date of birth (YYYYMMDD), 3-digit sequence code, 1-digit check code of digits or letters

Among them, it should be noted that the 15th and 16th digits in the date of birth represent the gender of the holder, with odd numbers representing males and even numbers representing females.

2. Use regular expressions to verify the ID number

When using PHP to verify the ID number, we can use the preg_match() function and regular expressions to determine the validity of the ID number. sex. The following is an example of a regular expression to determine the gender of an ID number:

function check_idcard($idcard){
    $idcard_pattern = '/^d{17}(d|x)$/i';
    if(preg_match($idcard_pattern, $idcard)){
        $sex_bit = substr($idcard, 16, 1);
        if($sex_bit % 2 != 0){
            echo '该身份证号码为男性。';
        }else{
            echo '该身份证号码为女性。';
        }
    }else{
        echo '该身份证号码不正确。';
    }
}

In the above function, we first use the regular expression "/^d{17}(d|x)$/i" to pass in Match the ID number to determine whether it meets the format requirements of the ID number. If the match is successful, the 17th digit of the ID number is extracted and the gender of the holder is determined based on parity. If the match is unsuccessful, an error message is output.

3. Usage example

The following is a usage example to verify the gender of the ID number:

$idcard = '510123199001011234';
check_idcard($idcard);

When the entered ID number is "510123199001011234", according to the ID number The 17th digit "1" in the number is an odd number, which means he is male. Therefore, the output result is "The ID number is male.".

4. Conclusion

Through the above introduction, we learned how to use PHP regular expressions to verify the gender of the ID number. In actual development, we can adjust regular expressions as needed to adapt to different business needs. At the same time, we also need to pay attention to the format and validity of the ID number to ensure the correctness and stability of the code.

The above is the detailed content of How to verify the gender of an ID number using PHP regular expressions. 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