PHP's ID card number tool class is translated into Java

WBOY
Release: 2016-07-25 08:50:59
Original
868 people have browsed it
Realize the conversion from 15-digit to 18-digit ID numbers, and verify the Mainland China Resident ID Card, Hong Kong Resident ID Card, Macau ID Card and Taiwan ID Card.
  1. /**
  2. * ID card tools
  3. *
  4. * @author Tongle Xu 2013-6-6
  5. * @copyright Copyright (c) 2003-2103 tintsoft.com
  6. * @license http://www .tintsoft.com
  7. * @version $Id$
  8. */
  9. class Utils_Idcard {
  10. /**
  11. * Minimum length of Chinese citizen ID number.
  12. */
  13. const CHINA_ID_MIN_LENGTH = 15;
  14. /**
  15. * Maximum length of Chinese citizen ID number .
  16. */
  17. const CHINA_ID_MAX_LENGTH = 18;
  18. /**
  19. * Minimum years
  20. */
  21. const MIN = 1930;
  22. /**
  23. *Province and municipality code table
  24. */
  25. 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" );
  26. /**
  27. * Weighting factor per bit
  28. */
  29. public static $power = array (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 );
  30. /**
  31. * The 18th digit of the check code
  32. */
  33. public static $verifyCode = array ("1","0","X","9","8","7","6","5","4","3","2" );
  34. /**
  35. *Domestic ID card verification
  36. */
  37. public static $cityCodes = array ('11' => '北京' );
  38. /**
  39. * Convert 15-digit ID number to 18 digits
  40. *
  41. * @param idCard 15-digit identity code
  42. * @return 18-digit identity code
  43. */
  44. public static function conver15CardTo18($idCard) {
  45. $idCard18 = "";
  46. if (strlen ( $idCard ) != self::CHINA_ID_MIN_LENGTH) {
  47. return null;
  48. }
  49. if (self::isNum ( $idCard )) {
  50. // 获取出生年月日
  51. $sYear = '19' . substr ( $idCard, 6, 2 );
  52. $idCard18 = substr ( $idCard, 0, 6 ) . $sYear . substr ( $idCard, 8 );
  53. // 转换字符数组
  54. $iArr = str_split ( $idCard18 );
  55. if ($iArr != null) {
  56. $iSum17 = self::getPowerSum ( $iArr );
  57. // 获取校验位
  58. $sVal = self::getCheckCode18 ( $iSum17 );
  59. if (strlen ( $sVal ) > 0) {
  60. $idCard18 .= $sVal;
  61. } else {
  62. return null;
  63. }
  64. }
  65. } else {
  66. return null;
  67. }
  68. return $idCard18;
  69. }
  70. /**
  71. * Verify whether the ID card is legal
  72. */
  73. public static function validateCard($idCard) {
  74. $card = trim ( $idCard );
  75. if (self::validateIdCard18 ( $card )) {
  76. return true;
  77. }
  78. if (self::validateIdCard15 ( $card )) {
  79. return true;
  80. }
  81. return false;
  82. }
  83. /**
  84. * Verify whether the 18-digit identity code is legal
  85. *
  86. * @param int $idCard identity code
  87. * @return boolean whether it is legal
  88. */
  89. public static function validateIdCard18($idCard) {
  90. $bTrue = false;
  91. if (strlen ( $idCard ) == self::CHINA_ID_MAX_LENGTH) {
  92. // 前17位
  93. $code17 = substr ( $idCard, 0, 17 );
  94. // 第18位
  95. $code18 = substr ( $idCard, 17, 1 );
  96. if (self::isNum ( $code17 )) {
  97. $iArr = str_split ( $code17 );
  98. if ($iArr != null) {
  99. $iSum17 = self::getPowerSum ( $iArr );
  100. // 获取校验位
  101. $val = self::getCheckCode18 ( $iSum17 );
  102. if (strlen ( $val ) > 0 && $val == $code18) {
  103. $bTrue = true;
  104. }
  105. }
  106. }
  107. }
  108. return $bTrue;
  109. }
  110. /**
  111. * Verify whether the 15-digit identity code is legal
  112. *
  113. * @param string $idCard identity code
  114. * @return boolean whether it is legal
  115. */
  116. public static function validateIdCard15($idCard) {
  117. if (strlen ( $idCard ) != self::CHINA_ID_MIN_LENGTH) {
  118. return false;
  119. }
  120. if (self::isNum ( $idCard )) {
  121. $proCode = substr ( $idCard, 0, 2 );
  122. if (! isset ( self::$cityCodes [$proCode] )) {
  123. return false;
  124. }
  125. //升到18位
  126. $idCard = self::conver15CardTo18($idCard);
  127. return self::validateIdCard18($idCard);
  128. } else {
  129. return false;
  130. }
  131. return true;
  132. }
  133. /**
  134. * Get age based on ID number
  135. *
  136. * @param string idCard ID number
  137. * @return age
  138. */
  139. public static function getAgeByIdCard($idCard) {
  140. $iAge = 0;
  141. if (strlen ( $idCard ) == self::CHINA_ID_MIN_LENGTH) {
  142. $idCard = self::conver15CardTo18 ( $idCard );
  143. }
  144. $year = substr ( $idCard, 6, 4 );
  145. $iCurrYear = date ( 'Y', time () );
  146. $iAge = $iCurrYear - $year;
  147. return $iAge;
  148. }
  149. /**
  150. * Get the birthday based on the identity number
  151. *
  152. * @param string $idCard identity number
  153. * @return NULL string
  154. */
  155. public static function getDateByIdCard($idCard) {
  156. $len = strlen ( $idCard );
  157. if ($len < self::CHINA_ID_MIN_LENGTH) {
  158. return null;
  159. } else if ($len == CHINA_ID_MIN_LENGTH) {
  160. $idCard = self::conver15CardTo18 ( $idCard );
  161. }
  162. return substr ( $idCard, 12, 2 );
  163. }
  164. /**
  165. * Get gender based on identity number
  166. *
  167. * @param string $idCard identity number
  168. * @return gender (M-male, F-female, N-unknown)
  169. */
  170. public static function getGenderByIdCard($idCard) {
  171. $sGender = "N";
  172. if (strlen ( $idCard ) == self::CHINA_ID_MIN_LENGTH) {
  173. $idCard = self::conver15CardTo18 ( $idCard );
  174. }
  175. $sCardNum = substr ( $idCard, 16, 1 );
  176. if (( int ) $sCardNum % 2 != 0) {
  177. $sGender = "M";
  178. } else {
  179. $sGender = "F";
  180. }
  181. return $sGender;
  182. }
  183. /**
  184. * Get the province of household registration based on the identity number
  185. *
  186. * @param string $idCard identity number
  187. * @return string
  188. */
  189. public static function getProvinceByIdCard($idCard) {
  190. $len = strlen ( $idCard );
  191. $sProvince = null;
  192. $sProvinNum = "";
  193. if ($len == self::CHINA_ID_MIN_LENGTH || $len == self::CHINA_ID_MAX_LENGTH) {
  194. $sProvinNum = substr ( $idCard, 0, 2 );
  195. }
  196. $sProvince = self::$cityCodes [$sProvinNum];
  197. return $sProvince;
  198. }
  199. /**
  200. *Number verification
  201. *
  202. * @param int $val
  203. */
  204. public static function isNum($val) {
  205. return $val == null || $val == "" ? false : 0 < preg_match ( '/^[0-9]*$/', $val );
  206. }
  207. /**
  208. * Verify whether a date smaller than the current date is valid
  209. *
  210. * @param int $iYear Date to be verified (year)
  211. * @param int $iMonth Date to be verified (month 1-12)
  212. * @param int $iDate Date to be verified ( Day)
  213. * @return Is it valid
  214. */
  215. public static function valiDate($iYear, $iMonth, $iDate) {
  216. $year = date ( 'Y', time () );
  217. if ($iYear < self::MIN || $iYear >= $year) {
  218. return false;
  219. }
  220. if ($iMonth < 1 || $iMonth > 12) {
  221. return false;
  222. }
  223. switch ($iMonth) {
  224. case 4 :
  225. case 6 :
  226. case 9 :
  227. case 11 :
  228. $datePerMonth = 30;
  229. break;
  230. case 2 :
  231. $dm = (($iYear % 4 == 0 && $iYear % 100 != 0) || ($iYear % 400 == 0)) && ($iYear > self::MIN && $iYear < $year);
  232. $datePerMonth = $dm ? 29 : 28;
  233. break;
  234. default :
  235. $datePerMonth = 31;
  236. }
  237. return ($iDate >= 1) && ($iDate <= $datePerMonth);
  238. }
  239. /**
  240. * After multiplying each bit of the ID card and the weighting factor of the corresponding bit, the sum value is obtained
  241. *
  242. * @param array $iArr
  243. * @return ID code.
  244. */
  245. private static function getPowerSum($iArr) {
  246. $iSum = 0;
  247. $power_len = count ( self::$power );
  248. $iarr_len = count ( $iArr );
  249. if ($power_len == $iarr_len) {
  250. for($i = 0; $i < $iarr_len; $i ++) {
  251. for($j = 0; $j < $power_len; $j ++) {
  252. if ($i == $j) {
  253. $iSum = $iSum + $iArr [$i] * self::$power [$j];
  254. }
  255. }
  256. }
  257. }
  258. return $iSum;
  259. }
  260. /**
  261. * Take the power sum value modulo 11 to get the remainder for check digit judgment
  262. *
  263. * @param int $iSum
  264. * @return check digit
  265. */
  266. private static function getCheckCode18($iSum) {
  267. $sCode = "";
  268. switch ($iSum % 11) {
  269. case 10 :
  270. $sCode = "2";
  271. break;
  272. case 9 :
  273. $sCode = "3";
  274. break;
  275. case 8 :
  276. $sCode = "4";
  277. break;
  278. case 7 :
  279. $sCode = "5";
  280. break;
  281. case 6 :
  282. $sCode = "6";
  283. break;
  284. case 5 :
  285. $sCode = "7";
  286. break;
  287. case 4 :
  288. $sCode = "8";
  289. break;
  290. case 3 :
  291. $sCode = "9";
  292. break;
  293. case 2 :
  294. $sCode = "x";
  295. break;
  296. case 1 :
  297. $sCode = "0";
  298. break;
  299. case 0 :
  300. $sCode = "1";
  301. break;
  302. }
  303. return $sCode;
  304. }
  305. }
复制代码
  1. /**
  2. * ID card tools
  3. *
  4. * @author Tongle Xu 2013-6-6
  5. * @copyright Copyright (c) 2003-2103 tintsoft.com
  6. * @license http://www .tintsoft.com
  7. * @version $Id$
  8. */
  9. class Utils_Idcard {
  10. /**
  11. * Minimum length of Chinese citizen ID number.
  12. */
  13. const CHINA_ID_MIN_LENGTH = 15;
  14. /**
  15. * Maximum length of Chinese citizen ID number .
  16. */
  17. const CHINA_ID_MAX_LENGTH = 18;
  18. /**
  19. * Minimum years
  20. */
  21. const MIN = 1930;
  22. /**
  23. *Province and municipality code table
  24. */
  25. 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" );
  26. /**
  27. * Weighting factor per bit
  28. */
  29. public static $power = array (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 );
  30. /**
  31. * The 18th digit of the check code
  32. */
  33. public static $verifyCode = array ("1","0","X","9","8","7","6","5","4","3","2" );
  34. /**
  35. *Domestic ID card verification
  36. */
  37. public static $cityCodes = array ('11' => 'Beijing','12' => 'Tianjin','13' => 'Hebei','14' => 'Shanxi','15' => 'Inner Mongolia','21' => 'Liaoning','22' => 'Jilin','23' => 'Heilongjiang','31' => 'Shanghai','32' => 'Jiangsu','33' => 'Zhejiang','34' => 'Anhui','35' => 'Fujian','36' => 'Jiangxi','37' => 'Shandong','41' => 'Henan','42' => 'Hubei','43' => 'Hunan','44' => 'Guangdong','45' => 'Guangxi',
  38. '46' => 'Hainan','50' => 'Chongqing','51' => 'Sichuan','52 ' => 'Guizhou','53' => 'Yunnan','54' => 'Tibet','61' => 'Shaanxi','62' => 'Gansu','15 ' => 'Inner Mongolia','21' => 'Liaoning','22' => 'Jilin','23' => 'Heilongjiang','31' => 'Shanghai','32 ' => 'Jiangsu','33' => 'Zhejiang','34' => 'Anhui','35' => 'Fujian','36' => 'Jiangxi','37 ' => 'Shandong','41' => 'Henan','42' => 'Hubei','43' => 'Hunan',
  39. '44' => 'Guangdong',' 45' => 'Guangxi','46' => 'Hainan','50' => 'Chongqing','51' => 'Sichuan','52' => 'Guizhou',' 53' => 'Yunnan','54' => 'Tibet','61' => 'Shaanxi','62' => 'Gansu','63' => 'Qinghai',' 64' => 'Ningxia','65' => 'Xinjiang','71' => 'Taiwan','81' => 'Hong Kong','82' => 'Macau',' 91' => 'Overseas','63' => 'Qinghai','64' => 'Ningxia','65' => 'Xinjiang','71' => 'Taiwan',' 81' => 'Hong Kong',
  40. '82' => 'Macau','91' => 'Abroad' );
  41. /**
  42. *Taiwan ID card verification
  43. *
  44. * @var array
  45. */
  46. public static $twFirstCode = array (' A' => 10,'B' => 11,'C' => 12,'D' => 13,'E' => 14,'F' => 15,'G' => 16,'H' => 17,'J' => 18,'K' => 19,'L' => 20,'M' => 21,'N' => ; 22,'P' => 23,'Q' => 24,'R' => 25,'S' => 26,'T' => 27,'U' => 28 ,'V' => 29,'X' => 30,'Y' => 31,'W' => 32,'Z' => 33,'I' => 34,' O' => 35 );
  47. /**
  48. * Hong Kong ID card verification
  49. */
  50. public static $hkFirstCode = array ('A' => 1,'B' => 2,'C' => 3, 'R' => 18,'U' => 21,'Z' => 26,'X' => 24,'W' => 23,'O' => 15,'N ' => 14 );
  51. /**
  52. * Convert 15-digit ID number to 18 digits
  53. *
  54. * @param idCard 15-digit identity code
  55. * @return 18-digit identity code
  56. */
  57. public static function conver15CardTo18($idCard) {
  58. $idCard18 = "";
  59. if (strlen ( $idCard ) != self::CHINA_ID_MIN_LENGTH) {
  60. return null;
  61. }
  62. if (self::isNum ( $idCard )) {
  63. // Get the year, month and day of birth
  64. $sYear = '19' . substr ( $idCard, 6, 2 );
  65. $idCard18 = substr ( $idCard, 0, 6 ) . $sYear . substr ( $idCard, 8 );
  66. // Convert character array
  67. $iArr = str_split ( $idCard18 );
  68. if ($iArr != null) {
  69. $iSum17 = self::getPowerSum ( $iArr );
  70. // Get the check digit
  71. $sVal = self::getCheckCode18 ( $iSum17 );
  72. if (strlen ( $sVal ) > 0) {
  73. $idCard18 .= $sVal;
  74. } else {
  75. return null;
  76. }
  77. }
  78. } else {
  79. return null;
  80. }
  81. return $idCard18;
  82. }
  83. /**
  84. * Verify whether the ID card is legal
  85. */
  86. public static function validateCard($idCard) {
  87. $card = trim ( $idCard );
  88. if (self::validateIdCard18 ( $card )) {
  89. return true;
  90. }
  91. if (self::validateIdCard15 ( $card )) {
  92. return true;
  93. }
  94. $cardval = self::validateIdCard10 ( $card );
  95. if ($cardval != null) {
  96. if ($cardval [2] == "true") {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * Verify whether the 18-digit identity code is legal
  104. *
  105. * @param int $idCard identity code
  106. * @return boolean whether it is legal
  107. */
  108. public static function validateIdCard18($idCard) {
  109. $bTrue = false;
  110. if (strlen ( $idCard ) == self::CHINA_ID_MAX_LENGTH) {
  111. // 前17位
  112. $code17 = substr ( $idCard, 0, 17 );
  113. // 第18位
  114. $code18 = substr ( $idCard, 17, 1 );
  115. if (self::isNum ( $code17 )) {
  116. $iArr = str_split ( $code17 );
  117. if ($iArr != null) {
  118. $iSum17 = self::getPowerSum ( $iArr );
  119. // 获取校验位
  120. $val = self::getCheckCode18 ( $iSum17 );
  121. if (strlen ( $val ) > 0 && $val == $code18) {
  122. $bTrue = true;
  123. }
  124. }
  125. }
  126. }
  127. return $bTrue;
  128. }
  129. /**
  130. * Verify whether the 15-digit identity code is legal
  131. *
  132. * @param string $idCard identity code
  133. * @return boolean whether it is legal
  134. */
  135. public static function validateIdCard15($idCard) {
  136. if (strlen ( $idCard ) != self::CHINA_ID_MIN_LENGTH) {
  137. return false;
  138. }
  139. if (self::isNum ( $idCard )) {
  140. $proCode = substr ( $idCard, 0, 2 );
  141. if (! isset ( self::$cityCodes [$proCode] )) {
  142. return false;
  143. }
  144. // 升到18位
  145. $idCard = self::conver15CardTo18 ( $idCard );
  146. return self::validateIdCard18 ( $idCard );
  147. } else {
  148. return false;
  149. }
  150. return true;
  151. }
  152. /**
  153. * Verify whether the 10-digit identity code is legal
  154. *
  155. * @param idCard identity code
  156. * @return ID card information array

  157. * [0] - Taiwan, Macau, Hong Kong [1] - Gender (Male M, Female F, unknown N) [2] - Is it legal (legal true, not legal false)
  158. * If it is not an ID number, return null
  159. *

  160. */
  161. public static function validateIdCard10($idCard) {
  162. $info = array ();
  163. $card = str_replace ( "[\(|\)]", "", $card );
  164. $len = strlen ( $card );
  165. if ($len != 8 && $len != 9 && $len != 10) {
  166. return null;
  167. }
  168. if (0 < preg_match ( "/^[a-zA-Z][0-9]{9}$/", $idCard )) { // 台湾
  169. $info [0] = "台湾";
  170. $char2 = substr ( $idCard, 1, 1 );
  171. if ($char2 == "1") {
  172. $info [1] = "M";
  173. } else if ($char2 == "2") {
  174. $info [1] = "F";
  175. } else {
  176. $info [1] = "N";
  177. $info [2] = "false";
  178. return $info;
  179. }
  180. $info [2] = self::validateTWCard ( $idCard ) ? "true" : "false";
  181. } else if (0 < preg_match ( "/^[1|5|7][0-9]{6}\(?[0-9A-Z]\)?$/", $idCard )) { // 澳门
  182. $info [0] = "澳门";
  183. $info [1] = "N";
  184. // TODO
  185. } else if (0 < preg_match ( "/^[A-Z]{1,2}[0-9]{6}\(?[0-9A]\)?$/", $idCard )) { // 香港
  186. $info [0] = "香港";
  187. $info [1] = "N";
  188. $info [2] = self::validateHKCard ( $idCard ) ? "true" : "false";
  189. } else {
  190. return null;
  191. }
  192. return info;
  193. }
  194. /**
  195. * Verify Taiwan ID card number
  196. *
  197. * @param string idCard ID card number
  198. * @return Verification code is consistent
  199. */
  200. public static function validateTWCard($idCard) {
  201. $start = substr ( $idCard, 0, 1 );
  202. $mid = substr ( $idCard, 1, 8 );
  203. $end = substr ( $idCard, 9, 1 );
  204. $iStart = self::$twFirstCode ['start'];
  205. $sum = $iStart / 10 + ($iStart % 10) * 9;
  206. $chars = str_split ( $mid );
  207. $iflag = 8;
  208. foreach ( $chars as $c ) {
  209. $sum = $sum + $c + "" * $iflag;
  210. $iflag --;
  211. }
  212. return ($sum % 10 == 0 ? 0 : (10 - $sum % 10)) == $end ? true : false;
  213. }
  214. /**
  215. * Verify Hong Kong ID card number (there is a bug, some special ID cards cannot be checked)
  216. *

  217. * The first 2 digits of the ID card are English characters. If only one English character appears, it means the first digit is a space. Corresponding to the number 58, the first two English characters A-Z correspond to the numbers 10-35
  218. respectively * The last digit of the check code is 0-9 plus the character "A", "A" represents 10
  219. *

  220. *

    * @return Whether the verification code matches

  221. */
  222. public static function validateHKCard($idCard) {
  223. $card = str_replace ( "[\(|\)]", "", $card );
  224. $sum = 0;
  225. if (strlen ( $card ) == 9) {
  226. $card0_arr = str_split ( strtoupper ( substr ( $card, 0, 1 ) ) );
  227. $card1_arr = str_split ( strtoupper ( substr ( $card, 1, 1 ) ) );
  228. $sum = ($card0_arr [0] - 55) * 9 . ($card1_arr [0] - 55) * 8;
  229. $card = substr ( $card, 1, 8 );
  230. } else {
  231. $card0_arr = str_split ( strtoupper ( substr ( $card, 0, 1 ) ) );
  232. $sum = 522 + ($card0_arr [0] - 55) * 8;
  233. }
  234. $mid = substr ( $card, 1, 6 );
  235. $end = substr ( $card, 7, 1 );
  236. $chars = str_split ( $mid );
  237. $iflag = 7;
  238. foreach ( $chars as $c ) {
  239. $sum = $sum + $c + "" * $iflag;
  240. $iflag --;
  241. }
  242. if (strtoupper ( $end ) == "A") {
  243. $sum = $sum + 10;
  244. } else {
  245. $sum = $sum + $end;
  246. }
  247. return ($sum % 11 == 0) ? true : false;
  248. }
  249. /**
  250. * Get age based on ID number
  251. *
  252. * @param string idCard ID number
  253. * @return age
  254. */
  255. public static function getAgeByIdCard($idCard) {
  256. $iAge = 0;
  257. if (strlen ( $idCard ) == self::CHINA_ID_MIN_LENGTH) {
  258. $idCard = self::conver15CardTo18 ( $idCard );
  259. }
  260. $year = substr ( $idCard, 6, 4 );
  261. $iCurrYear = date ( 'Y', time () );
  262. $iAge = $iCurrYear - $year;
  263. return $iAge;
  264. }
  265. /**
  266. * Get the birthday based on the identity number
  267. *
  268. * @param string $idCard identity number
  269. * @return NULL string
  270. */
  271. public static function getDateByIdCard($idCard) {
  272. $len = strlen ( $idCard );
  273. if ($len < self::CHINA_ID_MIN_LENGTH) {
  274. return null;
  275. } else if ($len == CHINA_ID_MIN_LENGTH) {
  276. $idCard = self::conver15CardTo18 ( $idCard );
  277. }
  278. return substr ( $idCard, 12, 2 );
  279. }
  280. /**
  281. * Get gender based on identity number
  282. *
  283. * @param string $idCard identity number
  284. * @return gender (M-male, F-female, N-unknown)
  285. */
  286. public static function getGenderByIdCard($idCard) {
  287. $sGender = "N";
  288. if (strlen ( $idCard ) == self::CHINA_ID_MIN_LENGTH) {
  289. $idCard = self::conver15CardTo18 ( $idCard );
  290. }
  291. $sCardNum = substr ( $idCard, 16, 1 );
  292. if (( int ) $sCardNum % 2 != 0) {
  293. $sGender = "M";
  294. } else {
  295. $sGender = "F";
  296. }
  297. return $sGender;
  298. }
  299. /**
  300. * Get the province of household registration based on the identity number
  301. *
  302. * @param string $idCard identity number
  303. * @return string
  304. */
  305. public static function getProvinceByIdCard($idCard) {
  306. $len = strlen ( $idCard );
  307. $sProvince = null;
  308. $sProvinNum = "";
  309. if ($len == self::CHINA_ID_MIN_LENGTH || $len == self::CHINA_ID_MAX_LENGTH) {
  310. $sProvinNum = substr ( $idCard, 0, 2 );
  311. }
  312. $sProvince = self::$cityCodes [$sProvinNum];
  313. return $sProvince;
  314. }
  315. /**
  316. *Number verification
  317. *
  318. * @param int $val
  319. */
  320. public static function isNum($val) {
  321. return $val == null || $val == "" ? false : 0 < preg_match ( '/^[0-9]*$/', $val );
  322. }
  323. /**
  324. * Verify whether a date smaller than the current date is valid
  325. *
  326. * @param int $iYear Date to be verified (year)
  327. * @param int $iMonth Date to be verified (month 1-12)
  328. * @param int $iDate Date to be verified ( Day)
  329. * @return Is it valid
  330. */
  331. public static function valiDate($iYear, $iMonth, $iDate) {
  332. $year = date ( 'Y', time () );
  333. if ($iYear < self::MIN || $iYear >= $year) {
  334. return false;
  335. }
  336. if ($iMonth < 1 || $iMonth > 12) {
  337. return false;
  338. }
  339. switch ($iMonth) {
  340. case 4 :
  341. case 6 :
  342. case 9 :
  343. case 11 :
  344. $datePerMonth = 30;
  345. break;
  346. case 2 :
  347. $dm = (($iYear % 4 == 0 && $iYear % 100 != 0) || ($iYear % 400 == 0)) && ($iYear > self::MIN && $iYear < $year);
  348. $datePerMonth = $dm ? 29 : 28;
  349. break;
  350. default :
  351. $datePerMonth = 31;
  352. }
  353. return ($iDate >= 1) && ($iDate <= $datePerMonth);
  354. }
  355. /**
  356. * 将身份证的每位和对应位的加权因子相乘之后,再得到和值
  357. *
  358. * @param array $iArr
  359. * @return 身份证编码。
  360. */
  361. private static function getPowerSum($iArr) {
  362. $iSum = 0;
  363. $power_len = count ( self::$power );
  364. $iarr_len = count ( $iArr );
  365. if ($power_len == $iarr_len) {
  366. for($i = 0; $i < $iarr_len; $i ++) {
  367. for($j = 0; $j < $power_len; $j ++) {
  368. if ($i == $j) {
  369. $iSum = $iSum + $iArr [$i] * self::$power [$j];
  370. }
  371. }
  372. }
  373. }
  374. return $iSum;
  375. }
  376. /**
  377. * Take the power sum value modulo 11 to get the remainder for check digit judgment
  378. *
  379. * @param int $iSum
  380. * @return check digit
  381. */
  382. private static function getCheckCode18($iSum) {
  383. $sCode = "";
  384. switch ($iSum % 11) {
  385. case 10 :
  386. $sCode = "2";
  387. break;
  388. case 9 :
  389. $sCode = "3";
  390. break;
  391. case 8 :
  392. $sCode = "4";
  393. break;
  394. case 7 :
  395. $sCode = "5";
  396. break;
  397. case 6 :
  398. $sCode = "6";
  399. break;
  400. case 5 :
  401. $sCode = "7";
  402. break;
  403. case 4 :
  404. $sCode = "8";
  405. break;
  406. case 3 :
  407. $sCode = "9";
  408. break;
  409. case 2 :
  410. $sCode = "x";
  411. break;
  412. case 1 :
  413. $sCode = "0";
  414. break;
  415. case 0 :
  416. $sCode = "1";
  417. break;
  418. }
  419. return $sCode;
  420. }
  421. }
复制代码


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