Home > Backend Development > PHP Tutorial > PHP regular expression to verify birthday information of ID number

PHP regular expression to verify birthday information of ID number

王林
Release: 2023-06-24 17:48:02
Original
1494 people have browsed it

ID card number is an identity proof tool often used in our daily life, and the birthday information contained in it is also very important. When using PHP to verify ID numbers, we often need to determine whether the birthday information is correct. This article will introduce how to use PHP regular expressions to verify the birthday information of the ID number.

1. The basic format of the ID number

The ID number is a string of 18 digits and letters. The last digit may be a number or a letter, and may be uppercase or lowercase. . The first 17 digits are the main part of the ID card, which contains the ID card's region, date of birth, gender and other information. The basic format of the ID number is as follows:

6-digit area code, 8-digit date of birth, 3-digit sequence code, 1-digit check code

Among them, the date of birth is in pure numeric format, Such as 19880101, so we can split the 18-digit string of the ID number into the following four parts through regular expressions:

$region_code // 6-digit region code
$birthday // 8-digit Year, month and day of birth
$sequence_code // 3-digit sequence code
$verify_code // 1-digit check code

2. PHP regular expression to verify birthday information

In verification When obtaining the birthday information of an ID number, we need to use regular expressions to extract the date, month, and year of birth, and then determine whether it conforms to the specified format.

In the ID card number, the position of the date of birth is from the 7th to the 14th, so we can use the substr() function to extract the 8-digit string, and then use regular expressions verify.

The following is the regular expression to verify the year, month and day of birth:

$birthday_pattern = '/^(19d{2}|20d{2})(0[1-9]|1 [012])(0[1-9]|[12]d|3[01])$/';

where:

^: indicates the beginning of the matching string
(19d{2}|20d{2}): Indicates matching the year 19XX or 20XX
(0[1-9]|1[012]): Indicates matching the months 01-12
(0[1 -9]|[12]d|3[01]): Indicates matching 01-31 days
$: Indicates matching the end of the string

Change the 8-digit date of birth in the ID number Match the above regular expression. If the match is successful, it means that the birthday information is legal.

The following is a complete PHP code example:

function validate_birthday($id_card)
{

$birthday_pattern = '/^(19d{2}|20d{2})(0[1-9]|1[012])(0[1-9]|[12]d|3[01])$/';

$birthday_string = substr($id_card, 6, 8);

if (preg_match($birthday_pattern, $birthday_string)) {
    return true;
} else {
    return false;
}
Copy after login

}

Use this function for identity When verifying the ID number, you only need to pass in the string of the ID number:

if (validate_birthday('440524199312220058')) {

echo '生日信息正确';
Copy after login

} else {

echo '生日信息错误';
Copy after login

}

Through the above method, we can quickly and accurately verify whether the birthday information of the ID number is legal, thereby ensuring the validity of the ID number.

The above is the detailed content of PHP regular expression to verify birthday information of ID number. 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