1 /** 2 * 验证身份证格式有效性 3 * @param type $id_card 4 * @return boolean 5 */ 6 function validation_filter_id_card($id_card) { 7 if (strlen($id_card) == 18) { 8 return idcard_checksum18($id_card); 9 } elseif ((strlen($id_card) == 15)) {10 $id_card = idcard_15to18($id_card);11 return idcard_checksum18($id_card);12 } else {13 return false;14 }15 }16 17 // 计算身份证校验码,根据国家标准GB 11643-199918 function idcard_verify_number($idcard_base) {19 if (strlen($idcard_base) != 17) {20 return false;21 }22 //加权因子23 $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);24 //校验码对应值25 $verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');26 $checksum = 0;27 for ($i = 0; $i < strlen($idcard_base); $i++) {28 $checksum += substr($idcard_base, $i, 1) * $factor[$i];29 }30 $mod = $checksum % 11;31 $verify_number = $verify_number_list[$mod];32 return $verify_number;33 }34 35 // 将15位身份证升级到18位36 function idcard_15to18($idcard) {37 if (strlen($idcard) != 15) {38 return false;39 } else {40 // 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码41 if (array_search(substr($idcard, 12, 3), array('996', '997', '998', '999')) !== false) {42 $idcard = substr($idcard, 0, 6) . '18' . substr($idcard, 6, 9);43 } else {44 $idcard = substr($idcard, 0, 6) . '19' . substr($idcard, 6, 9);45 }46 }47 $idcard = $idcard . idcard_verify_number($idcard);48 return $idcard;49 }50 51 // 18位身份证校验码有效性检查52 function idcard_checksum18($idcard) {53 if (strlen($idcard) != 18) {54 return false;55 }56 $idcard_base = substr($idcard, 0, 17);57 if (idcard_verify_number($idcard_base) != strtoupper(substr($idcard, 17, 1))) {58 return false;59 } else {60 return true;61 }62 }