PHP验证身份号码是否合法函数
<?php
/**
* 是否合法的身份证号
* @param string $idcard
* @return bool
*/
function is_idcard($idcard = '')
{
if (strlen($idcard) != 18) return false;
$idcard_base = substr($idcard, 0, 17);
if (strlen($idcard_base) != 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($idcard_base); $i++) {
$checksum += substr($idcard_base, $i, 1) * $factor[$i];
}
$mod = $checksum % 11;
$verify_number = $verify_number_list[$mod];
if ($verify_number == strtoupper(substr($idcard, 17, 1))) return true;
return false;
}
?>
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »