联系我们:416326801#qq.com(#改成@)

img1

PHP校验18位身份证号码

959 Views 标签: PHP

//校验身份证号码
public static function checkCardId($crad_id) { 
	if(strlen($crad_id) != 18) { 
		return false; 
	} 
	$crad_id_base = substr($crad_id, 0, 17); 
	if ( self::checkCardIdBase($crad_id_base) != strtoupper(substr($crad_id, 17, 1)) ){ 
		return false; 
	} else {
		return true;	
	}
} 


// 计算身份证校验码,根据国家标准GB 11643-1999 
private static function checkCardIdBase($crad_id_base) { 
	if(strlen($crad_id_base) != 17) { 
		return false; 
	} 
	//加权因子 
	$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); 
	//校验码对应值 
	$verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); 
	$checksum = 0; 
	for ($i = 0; $i < strlen($crad_id_base); $i++) 
	{ 
		$checksum += substr($crad_id_base, $i, 1) * $factor[$i]; 
	} 

	$mod = $checksum % 11; 
	$verify_number = $verify_number_list[$mod]; 
	return $verify_number; 
} 

如果觉得我的文章对您有用,打赏一下呗.

wxpay.png