文档:php判断身份证号.note
链接:http://note.youdao.com/noteshare?id=a23c10f6649ed0a0bca9c3f28958f1a9
function judge_id($my_id) { $id = strtoupper($my_id); $pattern = '/^\d{17}\d|X$/'; if (preg_match($pattern, $id)) { //定义身份证号前17位的校验权 $arr_right = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; //前17位运算后的正确校验码 $arr_valid = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2]; $sum = 0; for ($i = 0; $i < count($arr_right); $i++) { $sum += $id[$i] * $arr_right[$i]; } $check_code = $sum % 11; if ($id[17] == $arr_valid[$check_code]) { return 1; } else { return 0; } } else { return 0; } } $my_id='3709';//18位的身份证号 echo judge_id($my_id);