标签:
/**
check hk idcard
$id = "K548653A";
$xx= check_hkid($id);
var_dump($xx);
**/
function check_hkid($id)
{
if (!preg_match("/^[a-zA-Z][0-9]{6}[0-9aA]$/", $id)) {
return false;
}
$mul = 8;
$sum = (ord(strtoupper($id))-64) * $mul;
while($mul>1) {
$sum += intval(substr($id, 8 - $mul, 1)) * $mul;
$mul --;
}
$chksum = dechex(strval(11-($sum % 11)));//dec to hex
if ($chksum == ‘b‘) {
$chksum = 0;
}
return $chksum == strtolower(substr($id, 7, 1));
}
JS
private function check_hkid(id) {
if (!id.match(/^[a-zA-Z][0-9]{6}[0-9aA]$/)) return false;
var mul = 8, sum = (id.toUpperCase().charCodeAt()-64) * mul--;
while (mul>1) sum += parseInt(id.substr(8-mul, 1)) * mul--;
var chksum = (11-(sum % 11)).toString(16);
if (chksum == "b") chksum = "0";
return chksum == id.substr(7,1).toLowerCase();
}
标签:
原文地址:http://www.cnblogs.com/jyb2014/p/4218730.html