PHP 验证身份证号是否正确的函数

WBOY
Release: 2016-06-20 13:03:58
Original
1283 people have browsed it

PHP 验证身份证号是否正确的函数,函数代码如下:

<p>function validation_filter_id_card($id_card){</p>	if(strlen($id_card)==18){<br />		return idcard_checksum18($id_card);<br />	}elseif((strlen($id_card)==15)){<br />		$id_card=idcard_15to18($id_card);<br />		return idcard_checksum18($id_card);<br />	}else{<br />		return false;<br />	}<br />}<br />// 计算身份证校验码,根据国家标准GB 11643-1999 <br /><p>function idcard_verify_number($idcard_base){<br /></p>	if(strlen($idcard_base)!=17){<br />		return false;<br />	}<br />	//加权因子 <br />	$factor=array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);<br />	//校验码对应值 <br />	$verify_number_list=array('1','0','X','9','8','7','6','5','4','3','2');<br />	$checksum=0;<br />	for($i=0;$i<strlen($idcard_base);$i++){<br />		$checksum += substr($idcard_base,$i,1) * $factor[$i];<br />	}<br />	$mod=$checksum % 11;<br />	$verify_number=$verify_number_list[$mod];<br />	return $verify_number;<br />}<br />// 将15位身份证升级到18位 <br />function idcard_15to18($idcard){<br />	if(strlen($idcard)!=15){<br />		return false;<br />	}else{<br />		// 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码 <br />		if(array_search(substr($idcard,12,3),array('996','997','998','999')) !== false){<br />			$idcard=substr($idcard,0,6).'18'.substr($idcard,6,9);<br />		}else{<br />			$idcard=substr($idcard,0,6).'19'.substr($idcard,6,9);<br />		}<br />	}<br />	$idcard=$idcard.idcard_verify_number($idcard);<br />	return $idcard;<br />}<br />// 18位身份证校验码有效性检查 <br />function idcard_checksum18($idcard){<br />	if(strlen($idcard)!=18){<br />		return false;<br />	}<br />	$idcard_base=substr($idcard,0,17);<br />	if(idcard_verify_number($idcard_base)!=strtoupper(substr($idcard,17,1))){<br />		return false;<br />	}else{<br />		return true;<br />	}<br /><p>}</p>
Copy after login

调用方法如:

validation_filter_id_card('身份证号码');
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!