Home  >  Article  >  php教程  >  判断是否是身份证号

判断是否是身份证号

PHP中文网
PHP中文网Original
2016-05-23 16:38:581502browse

判断是否是身份证号

function isIdCard($number) { // 检查是否是身份证号
   $number = getIDCard($number);
   // 转化为大写,如出现x
   $number = strtoupper($number);
   //加权因子
   $wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
   //校验码串
   $ai = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
   //按顺序循环处理前17位
   $sigma = 0;
   for ($i = 0;$i < 17;$i++) {
    //提取前17位的其中一位,并将变量类型转为实数
    $b = (int) $number{$i};
 
    //提取相应的加权因子
    $w = $wi[$i];
 
    //把从身份证号码中提取的一位数字和加权因子相乘,并累加
    $sigma += $b * $w;
   }
   //计算序号
   $snumber = $sigma % 11;
 
   //按照序号从校验码串中提取相应的字符。
   $check_number = $ai[$snumber];
 
   if ($number{17} == $check_number) {
    return true;
   } else {
    return false;
   }
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn