Idcard.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. return true;
  56. if (!$this->get_shenfen($id)) {
  57. return false;
  58. }
  59. $len = strlen($id);
  60. if ($len == 18) {
  61. if (!$this->isChinaIDCardDate(substr($id, 6, 4), substr($id, 10, 2), substr($id, 12, 2))) {
  62. return false;
  63. }
  64. $code = $this->getValidateCode($id);
  65. if (strtoupper($code) == substr($id, 17, 1)) {
  66. return true;
  67. }
  68. return false;
  69. } else if ($len == 15) {
  70. if (!$this->isChinaIDCardDate('19' . substr($id, 6, 2), substr($id, 8, 2), substr($id, 10, 2))) {
  71. return false;
  72. }
  73. if (!is_numeric($id)) {
  74. return false;
  75. }
  76. return true;
  77. }
  78. return false;
  79. }
  80. /**
  81. * 根据身份证号,自动返回对应的性别
  82. */
  83. public function getChinaIDCardSex($cid)
  84. {
  85. $sexint = (int)substr($cid, 16, 1);
  86. return $sexint % 2 === 0 ? '女' : '男';
  87. }
  88. /**
  89. * 根据身份证号,自动返回对应的星座
  90. */
  91. public function getChinaIDCardXZ($cid)
  92. {
  93. $bir = substr($cid, 10, 4);
  94. $month = (int)substr($bir, 0, 2);
  95. $day = (int)substr($bir, 2);
  96. $strValue = '';
  97. if (($month == 1 && $day <= 21) || ($month == 2 && $day <= 19)) {
  98. $strValue = "水瓶座";
  99. } else if (($month == 2 && $day > 20) || ($month == 3 && $day <= 20)) {
  100. $strValue = "双鱼座";
  101. } else if (($month == 3 && $day > 20) || ($month == 4 && $day <= 20)) {
  102. $strValue = "白羊座";
  103. } else if (($month == 4 && $day > 20) || ($month == 5 && $day <= 21)) {
  104. $strValue = "金牛座";
  105. } else if (($month == 5 && $day > 21) || ($month == 6 && $day <= 21)) {
  106. $strValue = "双子座";
  107. } else if (($month == 6 && $day > 21) || ($month == 7 && $day <= 22)) {
  108. $strValue = "巨蟹座";
  109. } else if (($month == 7 && $day > 22) || ($month == 8 && $day <= 23)) {
  110. $strValue = "狮子座";
  111. } else if (($month == 8 && $day > 23) || ($month == 9 && $day <= 23)) {
  112. $strValue = "处女座";
  113. } else if (($month == 9 && $day > 23) || ($month == 10 && $day <= 23)) {
  114. $strValue = "天秤座";
  115. } else if (($month == 10 && $day > 23) || ($month == 11 && $day <= 22)) {
  116. $strValue = "天蝎座";
  117. } else if (($month == 11 && $day > 22) || ($month == 12 && $day <= 21)) {
  118. $strValue = "射手座";
  119. } else if (($month == 12 && $day > 21) || ($month == 1 && $day <= 20)) {
  120. $strValue = "魔羯座";
  121. }
  122. return $strValue;
  123. }
  124. /**
  125. * 根据身份证号,自动返回对应的生肖
  126. */
  127. public function getChinaIDCardSX($cid)
  128. {
  129. $start = 1901;
  130. $end = $end = (int)substr($cid, 6, 4);
  131. $x = ($start - $end) % 12;
  132. $value = "";
  133. if ($x == 1 || $x == -11) {
  134. $value = "鼠";
  135. }
  136. if ($x == 0) {
  137. $value = "牛";
  138. }
  139. if ($x == 11 || $x == -1) {
  140. $value = "虎";
  141. }
  142. if ($x == 10 || $x == -2) {
  143. $value = "兔";
  144. }
  145. if ($x == 9 || $x == -3) {
  146. $value = "龙";
  147. }
  148. if ($x == 8 || $x == -4) {
  149. $value = "蛇";
  150. }
  151. if ($x == 7 || $x == -5) {
  152. $value = "马";
  153. }
  154. if ($x == 6 || $x == -6) {
  155. $value = "羊";
  156. }
  157. if ($x == 5 || $x == -7) {
  158. $value = "猴";
  159. }
  160. if ($x == 4 || $x == -8) {
  161. $value = "鸡";
  162. }
  163. if ($x == 3 || $x == -9) {
  164. $value = "狗";
  165. }
  166. if ($x == 2 || $x == -10) {
  167. $value = "猪";
  168. }
  169. return $value;
  170. }
  171. /**
  172. * 根据身份证号,自动返回对应的省、自治区、直辖市名称
  173. */
  174. public function get_shenfen($id)
  175. {
  176. $index = substr($id, 0, 2);
  177. $area = array(
  178. 11 => "北京", 12 => "天津", 13 => "河北", 14 => "山西", 15 => "内蒙古", 21 => "辽宁",
  179. 22 => "吉林", 23 => "黑龙江", 31 => "上海", 32 => "江苏", 33 => "浙江", 34 => "安徽",
  180. 35 => "福建", 36 => "江西", 37 => "山东", 41 => "河南", 42 => "湖北", 43 => "湖南",
  181. 44 => "广东", 45 => "广西", 46 => "海南", 50 => "重庆", 51 => "四川", 52 => "贵州",
  182. 53 => "云南", 54 => "西藏", 61 => "陕西", 62 => "甘肃", 63 => "青海", 64 => "宁夏",
  183. 65 => "新疆", 71 => "台湾", 81 => "香港", 82 => "澳门", 91 => "国外"
  184. );
  185. return isset($area[$index]) ? $area[$index] : '';
  186. }
  187. public static function getInstance()
  188. {
  189. if (self::$_instance === null) {
  190. self::$_instance = new self();
  191. }
  192. return self::$_instance;
  193. }
  194. }