How to use PHP regular expressions to verify whether the input string is the correct ID number, passport number or Hong Kong and Macao pass format

PHPz
Release: 2023-06-24 14:28:01
Original
2517 people have browsed it

ID card, passport and Hong Kong and Macao pass numbers are all important personal identity certificates. In order to ensure the security of personal information, we need to verify in the system whether the ID number entered by the user complies with the standard format. PHP regular expressions are a very powerful tool that can easily achieve this purpose. This article will introduce how to use PHP regular expressions to verify the ID number, passport number and Hong Kong and Macao pass number entered by the user.

1. ID card number format verification

The ID number is an 18-digit number, and the last digit may be a number or the letter X. The first 17 digits of the ID number are a combination of area code and date of birth code, which can be verified using regular expressions. Let’s take a look at the regular expression for verifying ID number:

$reg = '/^d{17}[dX]$/';
Copy after login

where, ^ and $ represent the beginning and end of the string respectively. d represents a number, {17} means that the previous d must appear 17 times. [dX] means the last digit may be a number or the letter X. The part between / and / represents the content of the regular expression and can be adjusted as needed.

2. Passport number format verification

The Chinese passport number consists of 1 capital letter and 7 digits, and the foreign passport number consists of any digits of capital letters and digits. Let’s take a look at the regular expression for verifying passport numbers:

// 中国护照号码验证
$reg_cn_passport = '/^[A-Z]d{7}$/';
// 外国护照号码验证
$reg_foreign_passport = '/^[A-Z0-9]+$/';
Copy after login

Among them, [A-Z] means matching uppercase letters, d means matching numbers, and means matching one or more times. The two regular expressions above represent the verification rules for Chinese passport numbers and foreign passport numbers respectively.

3. Hong Kong and Macao Pass Number Format Verification

The Hong Kong and Macao Pass Number consists of 8 digits consisting of letters K or M. Let’s take a look at the regular expression for verifying the Hong Kong and Macao Pass Number:

$reg = '/^[KM]d{8}$/';
Copy after login

Among them, [KM] means matching K or M letters, d means matching numbers, and {8} means matching numbers 8 times. This regular expression is used to verify whether the format of the Hong Kong and Macao pass numbers is correct.

The above is the regular expression for verifying the format of ID card number, passport number and Hong Kong and Macao pass number. In database operations, form validation and other occasions, we can use these regular expressions for verification. PHP's preg_match() function can be used for regular expression matching. The specific usage is as follows:

// 身份证号码验证
$id_number = '123456789012345678'; // 待验证的身份证号码
$reg = '/^d{17}[dX]$/'; // 身份证号码验证正则表达式
if (preg_match($reg, $id_number)) {
    echo '身份证号码格式正确';
} else {
    echo '身份证号码格式错误';
}

// 护照号码验证
$passport_number = 'A1234567'; // 待验证的护照号码
$reg = '/^[A-Z]d{7}$/'; // 护照号码验证正则表达式
if (preg_match($reg, $passport_number)) {
    echo '护照号码格式正确';
} else {
    echo '护照号码格式错误';
}

// 港澳通行证号码验证
$hk_macau_passport_number = 'K12345678'; // 待验证的港澳通行证号码
$reg = '/^[KM]d{8}$/'; // 港澳通行证号码验证正则表达式
if (preg_match($reg, $hk_macau_passport_number)) {
    echo '港澳通行证号码格式正确';
} else {
    echo '港澳通行证号码格式错误';
}
Copy after login

The above code uses the preg_match() function to perform regular expression matching on ID number, passport number and Hong Kong and Macao pass number. , you can quickly determine whether the entered ID number conforms to the standard format.

Summary

This article introduces how to use PHP regular expressions to verify whether the ID number, passport number and Hong Kong and Macao pass number entered by the user conform to the standard format. Regular expressions are a very important skill and are widely used in processing text operations and form validation. By studying the content of this article, I believe you will have a deeper understanding of the application of PHP regular expressions.

The above is the detailed content of How to use PHP regular expressions to verify whether the input string is the correct ID number, passport number or Hong Kong and Macao pass format. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!