本文实例讲述了php正则判断是否为合法身份证号的方法。分享给大家供大家参考,具体如下:
/** * 判断是否为合法的身份证号码 * @param $mobile * @return int */ function iscreditno($vstr){ $vcity = array( '11','12','13','14','15','21','22', '23','31','32','33','34','35','36', '37','41','42','43','44','45','46', '50','51','52','53','54','61','62', '63','64','65','71','81','82','91' ); if (!preg_match('/^([d]{17}[xxd]|[d]{15})$/', $vstr)) return false; if (!in_array(substr($vstr, 0, 2), $vcity)) return false; $vstr = preg_replace('/[xx]$/i', 'a', $vstr); $vlength = strlen($vstr); if ($vlength == 18) { $vbirthday = substr($vstr, 6, 4) . '-' . substr($vstr, 10, 2) . '-' . substr($vstr, 12, 2); } else { $vbirthday = '19' . substr($vstr, 6, 2) . '-' . substr($vstr, 8, 2) . '-' . substr($vstr, 10, 2); } if (date('y-m-d', strtotime($vbirthday)) != $vbirthday) return false; if ($vlength == 18) { $vsum = 0; for ($i = 17 ; $i >= 0 ; $i--) { $vsubstr = substr($vstr, 17 - $i, 1); $vsum = (pow(2, $i) % 11) * (($vsubstr == 'a') ? 10 : intval($vsubstr , 11)); } if($vsum % 11 != 1) return false; } return true; }