Internet companies have an increasing demand for ID card verification. However, ordinary small companies are unable to connect to the identity authentication system of the public security department. Almost all certification services from some big Internet companies are purchased online. Even the cheaper certification price reaches 100,000 times/10,000 yuan. It only costs a dime a time, which is quite expensive. Therefore, in order to reduce the company's expenses, we should check the format of the ID card ourselves before getting the ID card information submitted by the user for authentication. If we are not satisfied with the most basic ID card format, we can directly reject it without having to Go through the next process of spending money to authenticate, and you can also create a user ID card table to save the data that passes each authentication. In the future, you will first query the user ID card table in your own system every time. If you cannot find it, then Go through the certification process to maximize company savings.
Here is a PHP ID card verification class that initially verifies the ID card format. This class only provides a static method isValid
interface, which is very simple to use. You only need to use IdentityCard::isValid
to verify. You can put it into your own project as a tool class. This method can simultaneously verify the 15-digit old ID card and the 18-digit new ID card format
PHP version: >=7.0.0
Note: If PHP For versions lower than 7.0, you only need to cancel the parameter restrictions on the methods in the class to use it.
class IdentityCard { /** * 校验身份证号是否合法 * @param string $num 待校验的身份证号 * @return bool */ public static function isValid(string $num) { //老身份证长度15位,新身份证长度18位 $length = strlen($num); if ($length == 15) { //如果是15位身份证 //15位身份证没有字母 if (!is_numeric($num)) { return false; } // 省市县(6位) $areaNum = substr($num, 0, 6); // 出生年月(6位) $dateNum = substr($num, 6, 6); } else if ($length == 18) { //如果是18位身份证 //基本格式校验 if (!preg_match('/^\d{17}[0-9xX]$/', $num)) { return false; } // 省市县(6位) $areaNum = substr($num, 0, 6); // 出生年月日(8位) $dateNum = substr($num, 6, 8); } else { //假身份证 return false; } //验证地区 if (!self::isAreaCodeValid($areaNum)) { return false; } //验证日期 if (!self::isDateValid($dateNum)) { return false; } //验证最后一位 if (!self::isVerifyCodeValid($num)) { return false; } return true; } /** * 省市自治区校验 * @param string $area 省、直辖市代码 * @return bool */ private static function isAreaCodeValid(string $area) { $provinceCode = substr($area, 0, 2); // 根据GB/T2260—999,省市代码11到65 if (11 <= $provinceCode && $provinceCode <= 65) { return true; } else { return false; } } /** * 验证出生日期合法性 * @param string $date 日期 * @return bool */ private static function isDateValid(string $date) { if (strlen($date) == 6) { //15位身份证号没有年份,这里拼上年份 $date = '19'.$date; } $year = intval(substr($date, 0, 4)); $month = intval(substr($date, 4, 2)); $day = intval(substr($date, 6, 2)); //日期基本格式校验 if (!checkdate($month, $day, $year)) { return false; } //日期格式正确,但是逻辑存在问题(如:年份大于当前年) $currYear = date('Y'); if ($year > $currYear) { return false; } return true; } /** * 验证18位身份证最后一位 * @param string $num 待校验的身份证号 * @return bool */ private static function isVerifyCodeValid(string $num) { if (strlen($num) == 18) { $factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; $tokens = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']; $checkSum = 0; for ($i = 0; $i < 17; $i++) { $checkSum += intval($num{$i}) * $factor[$i]; } $mod = $checkSum % 11; $token = $tokens[$mod]; $lastChar = strtoupper($num{17}); if ($lastChar != $token) { return false; } } return true; } }
Usage example:
// 测试 $num = '33071919610920021X'; if (IdentityCard::isValid($num)) { echo '身份证格式正确'; } else { echo '身份证格式不正确'; }
The following is the meaning of each part of the ID card encoding:
//=============新的18位身份证号码各位的含义:======================= //1-2位省、自治区、直辖市代码;11-65 //3-4位地级市、盟、自治州代码; //5-6位县、县级市、区代码; //7-14位出生年月日,比如19670401代表1967年4月1日; //15-17位为顺序号,其中17位男为单数,女为双数; //18位为校验码,0-9和X,由公式随机产生。 //举例: //130503 19670401 0012这个身份证号的含义: 13为河北,05为邢台,03为桥西区,出生日期为1967年4月1日,顺序号为001,2为验证码 //===========15位身份证号码各位的含义:======================= //1-2位省、自治区、直辖市代码; //3-4位地级市、盟、自治州代码; //5-6位县、县级市、区代码; //7-12位出生年月日,比如670401代表1967年4月1日,这是和18位号码的第一个区别; //13-15位为顺序号,其中15位男为单数,女为双数; //与18位身份证号的第二个区别:没有最后一位的验证码。 //举例: //130503 670401 001的含义; 13为河北,05为邢台,03为桥西区,出生日期为1967年4月1日,顺序号为001。
The above is the detailed content of PHP verify ID card format. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools