Idcard.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace common\helpers;
  3. /**
  4. * Class Idcard
  5. * @package common\helpers
  6. * @link https://laravel-china.org/articles/4769/php-identity-card-exact-match-verification
  7. */
  8. class Idcard
  9. {
  10. public $aWeight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; //十七位数字本体码权重
  11. public $aValidate = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']; //mod11,对应校验码字符值
  12. public $birthday;//出生年月
  13. public $sex;//性别
  14. public $xingzuo;//星座
  15. public $shuxiang;//属相
  16. /**
  17. * @var Idcard
  18. */
  19. private static $_instance;
  20. private function __construct()
  21. {
  22. }
  23. /**
  24. * 验证出生日期
  25. */
  26. public function isChinaIDCardDate($iY, $iM, $iD)
  27. {
  28. $iDate = $iY . '-' . $iM . '-' . $iD;
  29. $rPattern = '/^(([0-9]{2})|(19[0-9]{2})|(20[0-9]{2}))-((0[1-9]{1})|(1[012]{1}))-((0[1-9]{1})|(1[0-9]{1})|(2[0-9]{1})|3[01]{1})$/';
  30. if (preg_match($rPattern, $iDate, $arr)) {
  31. $this->birthday = $iDate;
  32. return true;
  33. }
  34. return false;
  35. }
  36. /**
  37. * 根据身份证号前17位, 算出识别码
  38. */
  39. public function getValidateCode($id)
  40. {
  41. $id17 = substr($id, 0, 17);
  42. $sum = 0;
  43. $len = strlen($id17);
  44. for ($i = 0; $i < $len; $i++) {
  45. $sum += $id17[$i] * $this->aWeight[$i];
  46. }
  47. $mode = $sum % 11;
  48. return $this->aValidate[$mode];
  49. }
  50. /**
  51. * 验证身份证号
  52. */
  53. public function isChinaIDCard($id)
  54. {
  55. if (!$this->get_shenfen($id)) {
  56. return false;
  57. }
  58. $len = strlen($id);
  59. if ($len == 18) {
  60. if (!$this->isChinaIDCardDate(substr($id, 6, 4), substr($id, 10, 2), substr($id, 12, 2))) {
  61. return false;
  62. }
  63. $code = $this->getValidateCode($id);
  64. if (strtoupper($code) == substr($id, 17, 1)) {
  65. return true;
  66. }
  67. return false;
  68. } else if ($len == 15) {
  69. if (!$this->isChinaIDCardDate('19' . substr($id, 6, 2), substr($id, 8, 2), substr($id, 10, 2))) {
  70. return false;
  71. }
  72. if (!is_numeric($id)) {
  73. return false;
  74. }
  75. return true;
  76. }
  77. return false;
  78. }
  79. /**
  80. * 根据身份证号,自动返回对应的性别
  81. */
  82. public function getChinaIDCardSex($cid)
  83. {
  84. $sexint = (int)substr($cid, 16, 1);
  85. return $sexint % 2 === 0 ? '女' : '男';
  86. }
  87. /**
  88. * 根据身份证号,自动返回对应的星座
  89. */
  90. public function getChinaIDCardXZ($cid)
  91. {
  92. $bir = substr($cid, 10, 4);
  93. $month = (int)substr($bir, 0, 2);
  94. $day = (int)substr($bir, 2);
  95. $strValue = '';
  96. if (($month == 1 && $day <= 21) || ($month == 2 && $day <= 19)) {
  97. $strValue = "水瓶座";
  98. } else if (($month == 2 && $day > 20) || ($month == 3 && $day <= 20)) {
  99. $strValue = "双鱼座";
  100. } else if (($month == 3 && $day > 20) || ($month == 4 && $day <= 20)) {
  101. $strValue = "白羊座";
  102. } else if (($month == 4 && $day > 20) || ($month == 5 && $day <= 21)) {
  103. $strValue = "金牛座";
  104. } else if (($month == 5 && $day > 21) || ($month == 6 && $day <= 21)) {
  105. $strValue = "双子座";
  106. } else if (($month == 6 && $day > 21) || ($month == 7 && $day <= 22)) {
  107. $strValue = "巨蟹座";
  108. } else if (($month == 7 && $day > 22) || ($month == 8 && $day <= 23)) {
  109. $strValue = "狮子座";
  110. } else if (($month == 8 && $day > 23) || ($month == 9 && $day <= 23)) {
  111. $strValue = "处女座";
  112. } else if (($month == 9 && $day > 23) || ($month == 10 && $day <= 23)) {
  113. $strValue = "天秤座";
  114. } else if (($month == 10 && $day > 23) || ($month == 11 && $day <= 22)) {
  115. $strValue = "天蝎座";
  116. } else if (($month == 11 && $day > 22) || ($month == 12 && $day <= 21)) {
  117. $strValue = "射手座";
  118. } else if (($month == 12 && $day > 21) || ($month == 1 && $day <= 20)) {
  119. $strValue = "魔羯座";
  120. }
  121. return $strValue;
  122. }
  123. /**
  124. * 根据身份证号,自动返回对应的生肖
  125. */
  126. public function getChinaIDCardSX($cid)
  127. {
  128. $start = 1901;
  129. $end = $end = (int)substr($cid, 6, 4);
  130. $x = ($start - $end) % 12;
  131. $value = "";
  132. if ($x == 1 || $x == -11) {
  133. $value = "鼠";
  134. }
  135. if ($x == 0) {
  136. $value = "牛";
  137. }
  138. if ($x == 11 || $x == -1) {
  139. $value = "虎";
  140. }
  141. if ($x == 10 || $x == -2) {
  142. $value = "兔";
  143. }
  144. if ($x == 9 || $x == -3) {
  145. $value = "龙";
  146. }
  147. if ($x == 8 || $x == -4) {
  148. $value = "蛇";
  149. }
  150. if ($x == 7 || $x == -5) {
  151. $value = "马";
  152. }
  153. if ($x == 6 || $x == -6) {
  154. $value = "羊";
  155. }
  156. if ($x == 5 || $x == -7) {
  157. $value = "猴";
  158. }
  159. if ($x == 4 || $x == -8) {
  160. $value = "鸡";
  161. }
  162. if ($x == 3 || $x == -9) {
  163. $value = "狗";
  164. }
  165. if ($x == 2 || $x == -10) {
  166. $value = "猪";
  167. }
  168. return $value;
  169. }
  170. /**
  171. * 根据身份证号,自动返回对应的省、自治区、直辖市代
  172. */
  173. public function get_shenfen($id)
  174. {
  175. $index = substr($id, 0, 2);
  176. $area = array(
  177. 11 => "北京", 12 => "天津", 13 => "河北", 14 => "山西", 15 => "内蒙古", 21 => "辽宁",
  178. 22 => "吉林", 23 => "黑龙江", 31 => "上海", 32 => "江苏", 33 => "浙江", 34 => "安徽",
  179. 35 => "福建", 36 => "江西", 37 => "山东", 41 => "河南", 42 => "湖北", 43 => "湖南",
  180. 44 => "广东", 45 => "广西", 46 => "海南", 50 => "重庆", 51 => "四川", 52 => "贵州",
  181. 53 => "云南", 54 => "西藏", 61 => "陕西", 62 => "甘肃", 63 => "青海", 64 => "宁夏",
  182. 65 => "新疆", 71 => "台湾", 81 => "香港", 82 => "澳门", 91 => "国外"
  183. );
  184. return isset($area[$index]) ? $area[$index] : '';
  185. }
  186. public static function getInstance()
  187. {
  188. if (self::$_instance === null) {
  189. self::$_instance = new self();
  190. }
  191. return self::$_instance;
  192. }
  193. }