


PHP regular expression to verify birthday information of ID number
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; }
}
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 '生日信息正确';
} else {
echo '生日信息错误';
}
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total
