PHP ID number tool class translation of that Java

Source: Internet
Author: User
Tags date array count idate min strlen valid access
<?php/** * ID Card Tools * * @author tongle Xu <xutongle@gmail.com> 2013-6-6 * @copyright Copyright (c) 2003-2103
	 tintsoft.com * @license http://www.php100.com * @version $Id $/class Utils_idcard {/** * Chinese citizen ID number minimum length.

	* * Const CHINA_ID_MIN_LENGTH = 15;
	 /** * China Citizen ID number maximum length.

	* * Const CHINA_ID_MAX_LENGTH = 18;

	/** * Minimum Age/Const min = 1930; /** * Province, the Central Municipality code table * * public static $cityCode = 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 ");

	/** * per-weighting factor/public static $power = Array (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);
	/** * 18th School Check code */public static $verifyCode = Array ("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");

	/** * Domestic ID CARD Verification * * public static $cityCodes = Array (' One ' => ' Beijing '); /** * Converts 15-bit ID number to 18-bit * * @param idcard 15-bit identity code * @return 18-bit identity code/public static function Conver15caRdTo18 ($idCard) {$idCard 18 = "";
		if (strlen ($idCard)!= self::china_id_min_length) {return null;
			if (Self::isnum ($idCard)) {//get Birth date $sYear = ' substr ' ($idCard, 6, 2); $idCard = substr ($idCard, 0, 6). $sYear.
			SUBSTR ($idCard, 8);
			Convert character Array $iArr = Str_split ($idCard 18);
				if ($IARR!= null) {$iSum = Self::getpowersum ($IARR);
				Get the check digit $sVal = self::getcheckcode18 ($iSum 17);
				if (strlen ($sVal) > 0) {$idCard. = $sVal;
				else {return null;
		}} else {return null;
	return $idCard 18;
		/** * Verify ID card is valid/public static function Validatecard ($idCard) {$card = Trim ($idCard);
		if (Self::validateidcard18 ($card)) {return true;
		} if (Self::validateidcard15 ($card)) {return true;
	return false; /** * Verify that the 18-bit identity code is valid * * @param int $idCard Identity Code * @return Boolean is legitimate/public static function Validateid Card18 ($idCard) {$bTrue = false;
			if (strlen ($idCard) = = Self::china_id_max_length) {//top 17 $code = substr ($idCard, 0, 17);
			18th $code = substr ($idCard, 17, 1);
				if (Self::isnum ($code)) {$IARR = Str_split ($code 17);
					if ($IARR!= null) {$iSum = Self::getpowersum ($IARR);
					Get the check digit $val = self::getcheckcode18 ($iSum 17);
					if (strlen ($val) > 0 && $val = = $code) {$bTrue = true;
	return $bTrue; /** * Verify that the 15-bit identity code is valid * * @param string $idCard Identity code * @return Boolean is legitimate/public static function Validat
		EIdCard15 ($idCard) {if (strlen ($idCard)!= self::china_id_min_length) {return false;
			} if (Self::isnum ($idCard)) {$proCode = substr ($idCard, 0, 2);
			if (! isset (self:: $cityCodes [$proCode]) {return false;
			}//Up to 18-digit $idCard = Self::conver15cardto18 ($idCard);
		Return Self::validateidcard18 ($idCard);
		else {return false; } return TRUe /** * Acquisition of age by identity number * * @param string Idcard ID * @return Age/public static function Getagebyidcard ($idCar
		D) {$iAge = 0;
		if (strlen ($idCard) = = self::china_id_min_length) {$idCard = Self::conver15cardto18 ($idCard);
		$year = substr ($idCard, 6, 4);
		$iCurrYear = Date (' Y ', Time ());
		$iAge = $iCurrYear-$year;
	return $iAge; /** * To obtain birthday * * * * @param string $idCard identity number * @return NULL string/public static function getdatebyi
		Dcard ($idCard) {$len = strlen ($idCard);
		if ($len < self::china_id_min_length) {return null;
		else if ($len = = china_id_min_length) {$idCard = Self::conver15cardto18 ($idCard);
	Return substr ($idCard, 12, 2); /** * Access to gender by identity number * * @param string $idCard identity number * @return sex (M-male, F-female, N-unknown)/public static function Getgen
		Derbyidcard ($idCard) {$sGender = "N"; if (strlen ($idCard) = = self::china_id_min_length) {$idCard = Self::conver15cardto18 ($idcard);
		$sCardNum = substr ($idCard, 16, 1);
		if ((int) $sCardNum% 2!= 0) {$sGender = "M";
		else {$sGender = "F";
	return $sGender; /** * Access to the registered province based on identity number * * @param string $idCard Identity number * @return string/public static function getprovincebyi
		Dcard ($idCard) {$len = strlen ($idCard);
		$sProvince = null;
		$sProvinNum = "";
		if ($len = = Self::china_id_min_length $len = = self::china_id_max_length) {$sProvinNum = substr ($idCard, 0, 2);
		$sProvince = self:: $cityCodes [$sProvinNum];
	return $sProvince; /** * Digital Verification * * @param int $val//public static function Isnum ($val) {return $val = = NULL $val = ""?
	false:0 < Preg_match ('/^[0-9]*$/', $val); /** * Validation is less than the current date is valid * * @param int $iYear to be verified date (year) * @param int $iMonth to be verified date (month 1-12) * @param int $iDate to be tested
		Certificate Date (DATE) * @return is valid/public static function ValiDate ($iYear, $iMonth, $iDate) {$year = date (' Y ', Time ()); if ($iYear < SELf::min $iYear >= $year) {return false;
		} if ($iMonth < 1 $iMonth >) {return false;
				Switch ($iMonth) {case 4:case 6:case 9:case: $datePerMonth = 30;
			Break  Case 2: $DM = (($iYear% 4 = 0 && $iYear%!= 0) ($iYear% = = 0)) && ($iYear > Self::min
				&& $iYear < $year); $datePerMonth = $DM?
				29:28;
			Break
		Default: $datePerMonth = 31;
	Return ($iDate >= 1) && ($iDate <= $datePerMonth);
	 /** * Multiplies each digit of the ID card and the weighted factor of the corresponding bit, and then obtains and the value * * @param array $IARR * @return ID card code.
		* * private static function Getpowersum ($IARR) {$iSum = 0;
		$power _len = count (self:: $power);
		$iarr _len = count ($IARR);
					if ($power _len = = $iarr _len) {for ($i = 0; $i < $iarr _len; $i + +) {for ($j = 0; $j < $power _len; $j + +) {
					if ($i = = $j) {$iSum = $iSum + $iArr [$i] * Self:: $power [$j];
	return $iSum; }/** * Will POWer and values and 11 modulo obtain the remainder for the check code judgment * * @param int $iSum * @return Check bit/private static function GetCheckCode18 ($iSum) {$sC
		Ode = "";
				Switch ($iSum%) {case: $sCode = "2";
			Break
				Case 9: $sCode = "3";
			Break
				Case 8: $sCode = "4";
			Break
				Case 7: $sCode = "5";
			Break
				Case 6: $sCode = "6";
			Break
				Case 5: $sCode = "7";
			Break
				Case 4: $sCode = "8";
			Break
				Case 3: $sCode = "9";
			Break
				Case 2: $sCode = "X";
			Break
				Case 1: $sCode = "0";
			Break
				Case 0: $sCode = "1";
		Break
	return $sCode; }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.