DeTechn Blog

PHP验证身份号码是否合法函数

<?php
/**
 * 是否合法的身份证号
 * @param string $idcard
 * @return bool
 */
function is_idcard($idcard = '')
{
    if (strlen($idcard) != 18) return false;
    $idcard_base&nbsp;=&nbsp;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&nbsp;=&nbsp;0;&nbsp;$i < strlen($idcard_base);&nbsp;$i++) {
        $checksum&nbsp;+=&nbsp;substr($idcard_base, $i,&nbsp;1)&nbsp;*&nbsp;$factor[$i];
    }
    $mod&nbsp;=&nbsp;$checksum % 11;
    $verify_number&nbsp;=&nbsp;$verify_number_list[$mod];
    if ($verify_number&nbsp;==&nbsp;strtoupper(substr($idcard, 17, 1))) return true;
    return false;
}
?>

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »