PHP function to verify that the ID card is valid

Source: Internet
Author: User
  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 bit
  17. {
  18. $REGX = "/^ (\d{6}) + (\d{2}) + (\d{2}) + (\d{2}) + (\d{3}) $/";
  19. @preg_match ($REGX, $id, $arr _split);
  20. Check that the birthday date is correct
  21. $dtm _birth = "+". $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]| X) $/";
  32. @preg_match ($REGX, $id, $arr _split);
  33. $dtm _birth = $arr _split[2]. '/' . $arr _split[3]. '/'. $arr _split[4];
  34. if (!strtotime ($dtm _birth))//Check whether the birthday date is correct
  35. {
  36. return FALSE;
  37. }
  38. Else
  39. {
  40. Verify that the check code for the 18-digit ID is correct.
  41. The check digit is generated according to ISO 7064:1983.mod 11-2, and X can be considered the number 10.
  42. $arr _int = Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  43. $arr _ch = Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 ');
  44. $sign = 0;
  45. for ($i = 0; $i < $i + +)
  46. {
  47. $b = (int) $id {$i};
  48. $w = $arr _int[$i];
  49. $sign + = $b * $W;
  50. }
  51. $n = $sign% 11;
  52. $val _num = $arr _ch[$n];
  53. if ($val _num! = substr ($id, 17, 1))
  54. {
  55. return FALSE;
  56. }
  57. Else
  58. {
  59. return TRUE;
  60. }
  61. }
  62. }
  63. }
Copy Code
is legal, PHP
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.