分享一个验证身份号函数:
function validateIdCard($value)
{
if (!preg_match('/^\d{17}[0-9xX]$/', $value)) { //基本格式校验
return false;
}
$parsed = date_parse(substr($value, 6, 8));
if (!(isset($parsed['warning_count'])
&& $parsed['warning_count'] == 0)) { //年月日位校验
return false;
}
$base = substr($value, 0, 17);
$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(substr($base, $i, 1)) * $factor[$i];
}
$mod = $checkSum % 11;
$token = $tokens[$mod];
$lastChar = strtoupper(substr($value, 17, 1));
return ($lastChar === $token); //最后一位校验位校验
}
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号