Maison > développement back-end > tutoriel php > PHP的身份证号码工具类翻译的那个Java的

PHP的身份证号码工具类翻译的那个Java的

WBOY
Libérer: 2016-07-25 08:50:59
original
887 Les gens l'ont consulté
实现从15位~18位的身份证号码转换,校验中国大陆居民身份证、香港居民身份证、澳门身份证和台湾身份证。
  1. /**
  2. * 身份证工具类
  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. * 中国公民身份证号码最小长度。
  12. */
  13. const CHINA_ID_MIN_LENGTH = 15;
  14. /**
  15. * 中国公民身份证号码最大长度。
  16. */
  17. const CHINA_ID_MAX_LENGTH = 18;
  18. /**
  19. * 最低年限
  20. */
  21. const MIN = 1930;
  22. /**
  23. * 省、直辖市代码表
  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. * 每位加权因子
  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. * 第18位校检码
  32. */
  33. public static $verifyCode = array ("1","0","X","9","8","7","6","5","4","3","2" );
  34. /**
  35. * 国内身份证校验
  36. */
  37. public static $cityCodes = array ('11' => '北京' );
  38. /**
  39. * 将15位身份证号码转换为18位
  40. *
  41. * @param idCard 15位身份编码
  42. * @return 18位身份编码
  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. * 验证身份证是否合法
  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. * 验证18位身份编码是否合法
  85. *
  86. * @param int $idCard 身份编码
  87. * @return boolean 是否合法
  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. * 验证15位身份编码是否合法
  112. *
  113. * @param string $idCard 身份编码
  114. * @return boolean 是否合法
  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. * 根据身份编号获取年龄
  135. *
  136. * @param string idCard 身份编号
  137. * @return 年龄
  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. * 根据身份编号获取生日天
  151. *
  152. * @param string $idCard 身份编号
  153. * @return NULL string
  154. */
  155. public static function getDateByIdCard($idCard) {
  156. $len = strlen ( $idCard );
  157. if ($len return null;
  158. } else if ($len == CHINA_ID_MIN_LENGTH) {
  159. $idCard = self::conver15CardTo18 ( $idCard );
  160. }
  161. return substr ( $idCard, 12, 2 );
  162. }
  163. /**
  164. * 根据身份编号获取性别
  165. *
  166. * @param string $idCard 身份编号
  167. * @return 性别(M-男,F-女,N-未知)
  168. */
  169. public static function getGenderByIdCard($idCard) {
  170. $sGender = "N";
  171. if (strlen ( $idCard ) == self::CHINA_ID_MIN_LENGTH) {
  172. $idCard = self::conver15CardTo18 ( $idCard );
  173. }
  174. $sCardNum = substr ( $idCard, 16, 1 );
  175. if (( int ) $sCardNum % 2 != 0) {
  176. $sGender = "M";
  177. } else {
  178. $sGender = "F";
  179. }
  180. return $sGender;
  181. }
  182. /**
  183. * 根据身份编号获取户籍省份
  184. *
  185. * @param string $idCard 身份编号
  186. * @return string
  187. */
  188. public static function getProvinceByIdCard($idCard) {
  189. $len = strlen ( $idCard );
  190. $sProvince = null;
  191. $sProvinNum = "";
  192. if ($len == self::CHINA_ID_MIN_LENGTH || $len == self::CHINA_ID_MAX_LENGTH) {
  193. $sProvinNum = substr ( $idCard, 0, 2 );
  194. }
  195. $sProvince = self::$cityCodes [$sProvinNum];
  196. return $sProvince;
  197. }
  198. /**
  199. * 数字验证
  200. *
  201. * @param int $val
  202. */
  203. public static function isNum($val) {
  204. return $val == null || $val == "" ? false : 0 }
  205. /**
  206. * 验证小于当前日期 是否有效
  207. *
  208. * @param int $iYear 待验证日期(年)
  209. * @param int $iMonth 待验证日期(月 1-12)
  210. * @param int $iDate 待验证日期(日)
  211. * @return 是否有效
  212. */
  213. public static function valiDate($iYear, $iMonth, $iDate) {
  214. $year = date ( 'Y', time () );
  215. if ($iYear = $year) {
  216. return false;
  217. }
  218. if ($iMonth 12) {
  219. return false;
  220. }
  221. switch ($iMonth) {
  222. case 4 :
  223. case 6 :
  224. case 9 :
  225. case 11 :
  226. $datePerMonth = 30;
  227. break;
  228. case 2 :
  229. $dm = (($iYear % 4 == 0 && $iYear % 100 != 0) || ($iYear % 400 == 0)) && ($iYear > self::MIN && $iYear $datePerMonth = $dm ? 29 : 28;
  230. break;
  231. default :
  232. $datePerMonth = 31;
  233. }
  234. return ($iDate >= 1) && ($iDate }
  235. /**
  236. * 将身份证的每位和对应位的加权因子相乘之后,再得到和值
  237. *
  238. * @param array $iArr
  239. * @return 身份证编码。
  240. */
  241. private static function getPowerSum($iArr) {
  242. $iSum = 0;
  243. $power_len = count ( self::$power );
  244. $iarr_len = count ( $iArr );
  245. if ($power_len == $iarr_len) {
  246. for($i = 0; $i for($j = 0; $j if ($i == $j) {
  247. $iSum = $iSum + $iArr [$i] * self::$power [$j];
  248. }
  249. }
  250. }
  251. }
  252. return $iSum;
  253. }
  254. /**
  255. * 将power和值与11取模获得余数进行校验码判断
  256. *
  257. * @param int $iSum
  258. * @return 校验位
  259. */
  260. private static function getCheckCode18($iSum) {
  261. $sCode = "";
  262. switch ($iSum % 11) {
  263. case 10 :
  264. $sCode = "2";
  265. break;
  266. case 9 :
  267. $sCode = "3";
  268. break;
  269. case 8 :
  270. $sCode = "4";
  271. break;
  272. case 7 :
  273. $sCode = "5";
  274. break;
  275. case 6 :
  276. $sCode = "6";
  277. break;
  278. case 5 :
  279. $sCode = "7";
  280. break;
  281. case 4 :
  282. $sCode = "8";
  283. break;
  284. case 3 :
  285. $sCode = "9";
  286. break;
  287. case 2 :
  288. $sCode = "x";
  289. break;
  290. case 1 :
  291. $sCode = "0";
  292. break;
  293. case 0 :
  294. $sCode = "1";
  295. break;
  296. }
  297. return $sCode;
  298. }
  299. }
复制代码
  1. /**
  2. * 身份证工具类
  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. * 中国公民身份证号码最小长度。
  12. */
  13. const CHINA_ID_MIN_LENGTH = 15;
  14. /**
  15. * 中国公民身份证号码最大长度。
  16. */
  17. const CHINA_ID_MAX_LENGTH = 18;
  18. /**
  19. * 最低年限
  20. */
  21. const MIN = 1930;
  22. /**
  23. * 省、直辖市代码表
  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. * 每位加权因子
  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. * 第18位校检码
  32. */
  33. public static $verifyCode = array ("1","0","X","9","8","7","6","5","4","3","2" );
  34. /**
  35. * 国内身份证校验
  36. */
  37. public static $cityCodes = array ('11' => '北京','12' => '天津','13' => '河北','14' => '山西','15' => '内蒙古','21' => '辽宁','22' => '吉林','23' => '黑龙江','31' => '上海','32' => '江苏','33' => '浙江','34' => '安徽','35' => '福建','36' => '江西','37' => '山东','41' => '河南','42' => '湖北','43' => '湖南','44' => '广东','45' => '广西',
  38. '46' => '海南','50' => '重庆','51' => '四川','52' => '贵州','53' => '云南','54' => '西藏','61' => '陕西','62' => '甘肃','15' => '内蒙古','21' => '辽宁','22' => '吉林','23' => '黑龙江','31' => '上海','32' => '江苏','33' => '浙江','34' => '安徽','35' => '福建','36' => '江西','37' => '山东','41' => '河南','42' => '湖北','43' => '湖南',
  39. '44' => '广东','45' => '广西','46' => '海南','50' => '重庆','51' => '四川','52' => '贵州','53' => '云南','54' => '西藏','61' => '陕西','62' => '甘肃','63' => '青海','64' => '宁夏','65' => '新疆','71' => '台湾','81' => '香港','82' => '澳门','91' => '国外','63' => '青海','64' => '宁夏','65' => '新疆','71' => '台湾','81' => '香港',
  40. '82' => '澳门','91' => '国外' );
  41. /**
  42. * 台湾身份证校验
  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. * 香港身份证校验
  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. * 将15位身份证号码转换为18位
  53. *
  54. * @param idCard 15位身份编码
  55. * @return 18位身份编码
  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. // 获取出生年月日
  64. $sYear = '19' . substr ( $idCard, 6, 2 );
  65. $idCard18 = substr ( $idCard, 0, 6 ) . $sYear . substr ( $idCard, 8 );
  66. // 转换字符数组
  67. $iArr = str_split ( $idCard18 );
  68. if ($iArr != null) {
  69. $iSum17 = self::getPowerSum ( $iArr );
  70. // 获取校验位
  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. * 验证身份证是否合法
  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. * 验证18位身份编码是否合法
  104. *
  105. * @param int $idCard 身份编码
  106. * @return boolean 是否合法
  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. * 验证15位身份编码是否合法
  131. *
  132. * @param string $idCard 身份编码
  133. * @return boolean 是否合法
  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. * 验证10位身份编码是否合法
  154. *
  155. * @param idCard 身份编码
  156. * @return 身份证信息数组

  157. * [0] - 台湾、澳门、香港 [1] - 性别(男M,女F,未知N) [2] - 是否合法(合法true,不合法false)
  158. * 若不是身份证件号码则返回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 $info [0] = "台湾";
  169. $char2 = substr ( $idCard, 1, 1 );
  170. if ($char2 == "1") {
  171. $info [1] = "M";
  172. } else if ($char2 == "2") {
  173. $info [1] = "F";
  174. } else {
  175. $info [1] = "N";
  176. $info [2] = "false";
  177. return $info;
  178. }
  179. $info [2] = self::validateTWCard ( $idCard ) ? "true" : "false";
  180. } else if (0 $info [0] = "澳门";
  181. $info [1] = "N";
  182. // TODO
  183. } else if (0 $info [0] = "香港";
  184. $info [1] = "N";
  185. $info [2] = self::validateHKCard ( $idCard ) ? "true" : "false";
  186. } else {
  187. return null;
  188. }
  189. return info;
  190. }
  191. /**
  192. * 验证台湾身份证号码
  193. *
  194. * @param string idCard 身份证号码
  195. * @return 验证码是否符合
  196. */
  197. public static function validateTWCard($idCard) {
  198. $start = substr ( $idCard, 0, 1 );
  199. $mid = substr ( $idCard, 1, 8 );
  200. $end = substr ( $idCard, 9, 1 );
  201. $iStart = self::$twFirstCode ['start'];
  202. $sum = $iStart / 10 + ($iStart % 10) * 9;
  203. $chars = str_split ( $mid );
  204. $iflag = 8;
  205. foreach ( $chars as $c ) {
  206. $sum = $sum + $c + "" * $iflag;
  207. $iflag --;
  208. }
  209. return ($sum % 10 == 0 ? 0 : (10 - $sum % 10)) == $end ? true : false;
  210. }
  211. /**
  212. * 验证香港身份证号码(存在Bug,部份特殊身份证无法检查)
  213. *

  214. * 身份证前2位为英文字符,如果只出现一个英文字符则表示第一位是空格,对应数字58 前2位英文字符A-Z分别对应数字10-35
  215. * 最后一位校验码为0-9的数字加上字符"A","A"代表10
  216. *
  217. *

  218. * 将身份证号码全部转换为数字,分别对应乘9-1相加的总和,整除11则证件号码有效
  219. *
  220. *
  221. * @param idCard 身份证号码
  222. * @return 验证码是否符合
  223. */
  224. public static function validateHKCard($idCard) {
  225. $card = str_replace ( "[\\(|\\)]", "", $card );
  226. $sum = 0;
  227. if (strlen ( $card ) == 9) {
  228. $card0_arr = str_split ( strtoupper ( substr ( $card, 0, 1 ) ) );
  229. $card1_arr = str_split ( strtoupper ( substr ( $card, 1, 1 ) ) );
  230. $sum = ($card0_arr [0] - 55) * 9 . ($card1_arr [0] - 55) * 8;
  231. $card = substr ( $card, 1, 8 );
  232. } else {
  233. $card0_arr = str_split ( strtoupper ( substr ( $card, 0, 1 ) ) );
  234. $sum = 522 + ($card0_arr [0] - 55) * 8;
  235. }
  236. $mid = substr ( $card, 1, 6 );
  237. $end = substr ( $card, 7, 1 );
  238. $chars = str_split ( $mid );
  239. $iflag = 7;
  240. foreach ( $chars as $c ) {
  241. $sum = $sum + $c + "" * $iflag;
  242. $iflag --;
  243. }
  244. if (strtoupper ( $end ) == "A") {
  245. $sum = $sum + 10;
  246. } else {
  247. $sum = $sum + $end;
  248. }
  249. return ($sum % 11 == 0) ? true : false;
  250. }
  251. /**
  252. * 根据身份编号获取年龄
  253. *
  254. * @param string idCard 身份编号
  255. * @return 年龄
  256. */
  257. public static function getAgeByIdCard($idCard) {
  258. $iAge = 0;
  259. if (strlen ( $idCard ) == self::CHINA_ID_MIN_LENGTH) {
  260. $idCard = self::conver15CardTo18 ( $idCard );
  261. }
  262. $year = substr ( $idCard, 6, 4 );
  263. $iCurrYear = date ( 'Y', time () );
  264. $iAge = $iCurrYear - $year;
  265. return $iAge;
  266. }
  267. /**
  268. * 根据身份编号获取生日天
  269. *
  270. * @param string $idCard 身份编号
  271. * @return NULL string
  272. */
  273. public static function getDateByIdCard($idCard) {
  274. $len = strlen ( $idCard );
  275. if ($len return null;
  276. } else if ($len == CHINA_ID_MIN_LENGTH) {
  277. $idCard = self::conver15CardTo18 ( $idCard );
  278. }
  279. return substr ( $idCard, 12, 2 );
  280. }
  281. /**
  282. * 根据身份编号获取性别
  283. *
  284. * @param string $idCard 身份编号
  285. * @return 性别(M-男,F-女,N-未知)
  286. */
  287. public static function getGenderByIdCard($idCard) {
  288. $sGender = "N";
  289. if (strlen ( $idCard ) == self::CHINA_ID_MIN_LENGTH) {
  290. $idCard = self::conver15CardTo18 ( $idCard );
  291. }
  292. $sCardNum = substr ( $idCard, 16, 1 );
  293. if (( int ) $sCardNum % 2 != 0) {
  294. $sGender = "M";
  295. } else {
  296. $sGender = "F";
  297. }
  298. return $sGender;
  299. }
  300. /**
  301. * 根据身份编号获取户籍省份
  302. *
  303. * @param string $idCard 身份编号
  304. * @return string
  305. */
  306. public static function getProvinceByIdCard($idCard) {
  307. $len = strlen ( $idCard );
  308. $sProvince = null;
  309. $sProvinNum = "";
  310. if ($len == self::CHINA_ID_MIN_LENGTH || $len == self::CHINA_ID_MAX_LENGTH) {
  311. $sProvinNum = substr ( $idCard, 0, 2 );
  312. }
  313. $sProvince = self::$cityCodes [$sProvinNum];
  314. return $sProvince;
  315. }
  316. /**
  317. * 数字验证
  318. *
  319. * @param int $val
  320. */
  321. public static function isNum($val) {
  322. return $val == null || $val == "" ? false : 0 }
  323. /**
  324. * 验证小于当前日期 是否有效
  325. *
  326. * @param int $iYear 待验证日期(年)
  327. * @param int $iMonth 待验证日期(月 1-12)
  328. * @param int $iDate 待验证日期(日)
  329. * @return 是否有效
  330. */
  331. public static function valiDate($iYear, $iMonth, $iDate) {
  332. $year = date ( 'Y', time () );
  333. if ($iYear = $year) {
  334. return false;
  335. }
  336. if ($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 $datePerMonth = $dm ? 29 : 28;
  348. break;
  349. default :
  350. $datePerMonth = 31;
  351. }
  352. return ($iDate >= 1) && ($iDate }
  353. /**
  354. * 将身份证的每位和对应位的加权因子相乘之后,再得到和值
  355. *
  356. * @param array $iArr
  357. * @return 身份证编码。
  358. */
  359. private static function getPowerSum($iArr) {
  360. $iSum = 0;
  361. $power_len = count ( self::$power );
  362. $iarr_len = count ( $iArr );
  363. if ($power_len == $iarr_len) {
  364. for($i = 0; $i for($j = 0; $j if ($i == $j) {
  365. $iSum = $iSum + $iArr [$i] * self::$power [$j];
  366. }
  367. }
  368. }
  369. }
  370. return $iSum;
  371. }
  372. /**
  373. * 将power和值与11取模获得余数进行校验码判断
  374. *
  375. * @param int $iSum
  376. * @return 校验位
  377. */
  378. private static function getCheckCode18($iSum) {
  379. $sCode = "";
  380. switch ($iSum % 11) {
  381. case 10 :
  382. $sCode = "2";
  383. break;
  384. case 9 :
  385. $sCode = "3";
  386. break;
  387. case 8 :
  388. $sCode = "4";
  389. break;
  390. case 7 :
  391. $sCode = "5";
  392. break;
  393. case 6 :
  394. $sCode = "6";
  395. break;
  396. case 5 :
  397. $sCode = "7";
  398. break;
  399. case 4 :
  400. $sCode = "8";
  401. break;
  402. case 3 :
  403. $sCode = "9";
  404. break;
  405. case 2 :
  406. $sCode = "x";
  407. break;
  408. case 1 :
  409. $sCode = "0";
  410. break;
  411. case 0 :
  412. $sCode = "1";
  413. break;
  414. }
  415. return $sCode;
  416. }
  417. }
复制代码


source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal