A function that verifies that the ID card is valid in PHP

Source: Internet
Author: User
This paper mainly introduces the function of verifying the identity card of PHP. Have a good reference value, follow the small series together to see it

If you don't say much, look at the code:

function Is_idcard ($id) {$id = Strtoupper ($id); $REGX = "/(^\d{15}$) | (^\d{17} ([0-9]|  X) $)/";  $arr _split = Array ();  if (!preg_match ($REGX, $id)) {return FALSE;    } if (15==strlen ($id))//Check 15-bit {$regx = "/^ (\d{6}) + (\d{2}) + (\d{2}) + (\d{2}) + (\d{3}) $/";    @preg_match ($REGX, $id, $arr _split); Check that the birthday date is correct $dtm _birth = "+". $arr _split[2]. '/' . $arr _split[3].    '/'. $arr _split[4];    if (!strtotime ($dtm _birth)) {return FALSE;    } else {return TRUE; }} else//check 18-bit {$regx = "/^ (\d{6}) + (\d{4}) + (\d{2}) + (\d{2}) + (\d{3}) ([0-9]|    X) $/";    @preg_match ($REGX, $id, $arr _split); $dtm _birth = $arr _split[2]. '/' . $arr _split[3].    '/'. $arr _split[4];    if (!strtotime ($dtm _birth))//Check whether the birthday date is correct {return FALSE;      } else {//verify that the check code for the 18-bit ID is correct.      The check digit is generated according to ISO 7064:1983.mod 11-2, and X can be considered the number 10.      $arr _int = Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); $arr _ch = Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', '2 ');      $sign = 0;        for ($i = 0; $i < $i + +) {$b = (int) $id {$i};        $w = $arr _int[$i];      $sign + = $b * $W;      } $n = $sign% 11;      $val _num = $arr _ch[$n];      if ($val _num! = substr ($id, 1)) {return FALSE;      } else {return TRUE; }    }  }}

The above is the whole content of this article, I hope that everyone's study has helped.


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.