驗證身份證號
function is_idcard( $id ) {
$id = strtoupper($id);
$regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
$arr_split = array();
if(!preg_match($regx, $id)) return false;
if(15==strlen($id)){
$regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
@preg_match($regx, $id, $arr_split);
$dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
if(!strtotime($dtm_birth)) return false;
return true;
} else {
$regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
@preg_match($regx, $id, $arr_split);
$dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
if(!strtotime($dtm_birth)) return false;
$id_card_base = substr($id, 0, 17);
$verify_code = substr($id, 17, 1);
$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
$verify_code_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
$total = 0;
for ( $i = 0; $i < 17; $i++ ) {
$total += substr($id_card_base, $i, 1)*$factor[$i];
}
$n = $total % 11;
$val_num = $verify_code_list[$n];
if ($val_num != substr($id,17, 1) || $val_num != $verify_code) return false;
return true;
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結