以前我要验证身份证我们多半会使用正则判断用户输入是不是15位或18位的全数字,然后来判断身份证是否合法了,这种方法只是最基础的,下面提供的这个身份证验证,可以识别真假身份证.
代码如下:
<?php $IDCard = new IDCard(); var_dump($IDCard::isCard($_GET['card'])); /** * 身份证处理类 */ class IDCard { //检证身份证是否正确 public static function isCard($card) { $card = self::to18Card($card); if (strlen($card) != 18) { return false; } $cardBase = substr($card, 0, 17); return (self::getVerifyNum($cardBase) == strtoupper(substr($card, 17, 1))); } //格式化15位身份证号码为18位 public static function to18Card($card) { $card = trim($card); if (strlen($card) == 18) { return $card; } if (strlen($card) != 15) { return false; } // 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码 if (array_search(substr($card, 12, 3) , array( '996', '997', '998', '999' )) !== false) { $card = substr($card, 0, 6) . '18' . substr($card, 6, 9); } else { $card = substr($card, 0, 6) . '19' . substr($card, 6, 9); } $card = $card . self::getVerifyNum($card); return $card; } // 计算身份证校验码,根据国家标准gb 11643-1999 private static function getVerifyNum($cardBase) { if (strlen($cardBase) != 17) { return false; } // 加权因子 $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); // 校验码对应值 $verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); $checksum = 0; for ($i = 0; $i < strlen($cardBase); $i++) { $checksum+= substr($cardBase, $i, 1) * $factor[$i]; } $mod = $checksum % 11; $verify_number = $verify_number_list[$mod]; return $verify_number; } }
文章网址:
随意转载^^但请附上教程地址。
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

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
Roblox: Grow A Garden - Complete Mutation Guide
3 weeks agoByDDD
Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
How to fix KB5055612 fails to install in Windows 10?
3 weeks agoByDDD
Blue Prince: How To Get To The Basement
1 months agoByDDD
Nordhold: Fusion System, Explained
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment
