Home > Backend Development > PHP Tutorial > PHP function to verify whether the ID card is legal

PHP function to verify whether the ID card is legal

WBOY
Release: 2016-07-25 08:42:28
Original
1153 people have browsed it
Is it legal, PHP
  1. /**
  2. * ID card
  3. *
  4. * @param string $id
  5. * @return boolean
  6. */
  7. function is_idcard( $id )
  8. {
  9. $id = strtoupper($id);
  10. $regx = "/(^d{15}$)|(^d{ 17}([0-9]|X)$)/";
  11. $arr_split = array();
  12. if(!preg_match($regx, $id))
  13. {
  14. return FALSE;
  15. }
  16. if(15= =strlen($id)) //Check 15 digits
  17. {
  18. $regx = "/^(d{6})+(d{2})+(d{2})+(d{2})+( d{3})$/";
  19. @preg_match($regx, $id, $arr_split);
  20. //Check whether the birthday date is correct
  21. $dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
  22. if(!strtotime($dtm_birth))
  23. {
  24. return FALSE;
  25. } else {
  26. return TRUE;
  27. }
  28. }
  29. else //Check 18 Bit
  30. {
  31. $regx = "/^(d{6})+(d{4})+(d{2})+(d{2})+(d{3})([0-9] | ;
  32. if(!strtotime($dtm_birth)) //Check whether the birthday date is correct
  33. {
  34. return FALSE;
  35. }
  36. else
  37. {
  38. //Check whether the check code of the 18-digit ID card is correct.
  39. //The check digit is generated in accordance with ISO 7064:1983.MOD 11-2. X can be considered as the number 10.
  40. $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  41. $arr_ch = array('1' , '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  42. $sign = 0;
  43. for ( $i = 0; $i < 17; $i++ )
  44. {
  45. $b = (int) $id{$i};
  46. $w = $arr_int[$i];
  47. $sign += $b * $w;
  48. }
  49. $n = $sign % 11;
  50. $val_num = $arr_ch[$n];
  51. if ($val_num != substr($id,17, 1))
  52. {
  53. return FALSE;
  54. }
  55. else
  56. {
  57. return TRUE;
  58. }
  59. }
  60. }
  61. }
Copy code

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