Home > Backend Development > PHP Tutorial > Mainland ID card number processing class

Mainland ID card number processing class

WBOY
Release: 2016-07-25 08:43:57
Original
1767 people have browsed it
Including the conversion of 15-digit mainland ID number to 18-digit, ID number validity check and ID card information extraction.
  1. use DateTime;
  2. /**
  3. * ID card number processing class
  4. * @author WeakSun <52132522@qq.com>
  5. */
  6. class Idcard {
  7. static private $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2),
  8. $verify = array(1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2),
  9. $info = array('status' => false, 'info' => '', 'data' => array());
  10. /**
  11. * Verify the validity of the ID number
  12. * @param string $idnum ID number
  13. * @return array
  14. */
  15. public function index($idnum = null) {
  16. switch (strlen($idnum)) {
  17. case 18:
  18. break;
  19. case 15:
  20. $idnum = $this->upgrade($idnum);
  21. break;
  22. default :
  23. return $this ->info(false, 'error', $idnum);
  24. }
  25. $info = $this->getInfo($idnum);
  26. $info['status'] = $this->getCode($idnum ) == strtoupper(substr($idnum, -1));
  27. return $info;
  28. }
  29. /**
  30. * Get ID card information
  31. * @param string $idnum ID number
  32. * @return array
  33. */
  34. public function getInfo($idnum) {
  35. $data = array(
  36. 'id ' => $idnum,
  37. 'city' => substr($idnum, 0, 6),
  38. 'birthday' => substr($idnum, 6, 4) . '-' . substr($idnum, 10, 2) . '-' . substr($idnum, 10, 2),
  39. 'sex' => substr($idnum, -2, 1) % 2 ? 'Male' : 'Female'
  40. );
  41. $datetime = new DateTime($data['birthday']);
  42. $data['age'] = $datetime->diff(date_create())->format('%y');
  43. return $this ->info(true, null, $data);
  44. }
  45. /**
  46. * 15-digit number upgraded to 18 digits
  47. * @param string $idnum
  48. * @return array
  49. */
  50. public function upgrade($idnum) {
  51. if (strlen($idnum) != 15) {
  52. $this ->info(true, null, $idnum);
  53. } else {
  54. $idnum = array(substr($idnum, 0, 6), 19, substr($idnum, 6));
  55. substr($idnum, -3) > 995 && $idnum[1] --;
  56. $idnum = implode('', $idnum);
  57. }
  58. return $this->info(true, null, $idnum . $this-> ;getCode($idnum));
  59. }
  60. /**
  61. * Get confirmation code
  62. * @param string $idnum ID number
  63. * @return string Return confirmation code if successful, false if failed
  64. */
  65. protected function getCode($idnum) {
  66. $code = 0;
  67. foreach (str_split($idnum) as $k => $ v) {
  68. $code += $v * static::$factor[$k];
  69. }
  70. $index = $code % 11;
  71. return static::$verify[$index];
  72. }
  73. protected function info($status, $msg = null, $data = null) {
  74. $info = static::$info;
  75. empty($msg) || $info['info'] = $msg;
  76. empty($data ) || $info['data'] = $data;
  77. if(is_array($status)){
  78. $this->info = array_merge($info, $status);
  79. }elseif(is_bool($status) ){
  80. $info['status'] = $status;
  81. }
  82. return $this->info;
  83. }
  84. }
Copy code


Related labels:
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