本文主要是介绍PHP 验证身份号码 包括15位18位,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
查了很多资料 发现网上身份证15位的验证并不是那么严谨 今天研究了一下 代码如下
<?php
function check_id_card($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 (!isAreaCodeValid($areaNum)) {return false;}//验证日期if (!isDateValid($dateNum)) {return false;}//验证最后一位if (!isVerifyCodeValid($num)) {return false;}return true;
}/*** 省市自治区校验* @param string $area 省、直辖市代码* @return bool*/
function isAreaCodeValid(string $area) {$provinceCode = substr($area, 0, 2);// 根据GB/T2260—999,省市代码11到65if (11 <= $provinceCode && $provinceCode <= 65) {return true;} else {return false;}
}/*** 验证出生日期合法性* @param string $date 日期* @return bool*/
function isDateValid($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*/function isVerifyCodeValid($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;}$a = '830503670401001';echo $a;var_dump(is_ck($a));echo PHP_EOL;$b = '430503670401001';echo $b;var_dump(is_ck($b));echo PHP_EOL;$c = '130503670401001';echo $c;var_dump(is_ck($c));echo PHP_EOL;
方法二
function is_idcard($id)
{$id = strtoupper($id);$regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";$arr_split = array();if (!preg_match($regx, $id)) {return FALSE;}if (15 == strlen($id)) {//检查15位$regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";@preg_match($regx, $id, $arr_split);//检查生日日期是否正确$dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];if (!strtotime($dtm_birth)) {return FALSE;} else {return TRUE;}} else {//检查18位$regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";@preg_match($regx, $id, $arr_split);$dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];if (!strtotime($dtm_birth)) {//检查生日日期是否正确return FALSE;} else {//检验18位身份证的校验码是否正确。//校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。$arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);$arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');$sign = 0;for ($i = 0; $i < 17; $i++) {$b = (int)$id{$i};$w = $arr_int[$i];$sign += $b * $w;}$n = $sign % 11;$val_num = $arr_ch[$n];if ($val_num != substr($id, 17, 1)) {return FALSE;} else {return TRUE;}}}
}
这篇关于PHP 验证身份号码 包括15位18位的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!