Home >php教程 >PHP源码 >PHP身份证号码15位升级为18位

PHP身份证号码15位升级为18位

PHP中文网
PHP中文网Original
2016-05-25 17:05:041217browse

身份证号码15位升级为18位,一个函数搞定

/**
	 * 功能:把15位身份证转换成18位
	 *
	 * @param string $idCard
	 * @return newid or id
	 */
	function getIDCard($idCard) {
		// 若是15位,则转换成18位;否则直接返回ID
		if (15 == strlen ( $idCard )) {
			$W = array (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1 );
			$A = array ("1","0","X","9","8","7","6","5","4","3","2" );
			$s = 0;
			$idCard18 = substr ( $idCard, 0, 6 ) . "19" . substr ( $idCard, 6 );
			$idCard18_len = strlen ( $idCard18 );
			for($i = 0; $i < $idCard18_len; $i ++) {
				$s = $s + substr ( $idCard18, $i, 1 ) * $W [$i];
			}
			$idCard18 .= $A [$s % 11];
			return $idCard18;
		} else {
			return $idCard;
		}
	}

                   


Statement:
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
Previous article:中文分词APINext article:PHP在线生成二维码