PHP implements a function to verify the correctness of the ID card number

Source: Internet
Author: User
This article mainly for you to share the two paragraph PHP verification ID card number is the correct function, interested in small partners can refer to

ID Card Verification The simplest is the length of the judgment, of course, this is very simple to break if you want more rules we need to do some of the ID card number according to the generated rules to verify, the following sorting out some of the functions we have to look at.

Example, share today a PHP Verified ID card number is the correct function.

/********************php Verify that the ID number is correct function *********************/functions 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;       }//phpfensi.com else {return TRUE; }     }   }  }

Calling the identity card validation function
$IDC =is_idcard ("identification number");
if ($IDC) {echo "correct";} Else{echo "Error";}

Example two:

function Validation_filter_id_card ($id _card) {if (strlen ($id _card) ==18) {return idcard_checksum18 ($id _card);  }elseif ((strlen ($id _card) ==15) {$id _card=idcard_15to18 ($id _card);  Return Idcard_checksum18 ($id _card);  }else{return false; }}//Calculate ID Check code, according to national standard GB 11643-1999 function Idcard_verify_number ($idcard _base) {if (strlen ($idcard _base)!=17) {return F  Alse;  }//Weighted factor $factor =array (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);  Parity codes correspond to values $verify _number_list=array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 ');  $checksum = 0;  for ($i =0; $i <strlen ($idcard _base), $i + +) {$checksum + = substr ($idcard _base, $i, 1) * $factor [$i];  } $mod = $checksum% 11;  $verify _number= $verify _number_list[$mod]; return $verify _number;  }//Upgrade 15-bit ID to 18-bit function idcard_15to18 ($idcard) {if (strlen ($idcard)!=15) {return false; }else{//If the ID sequence code is 996 997 998 999, these are special codes for centenarians older than if (Array_search (substr ($idcard, 12,3), Array (' 996 ', ' 997 ', ' 998 ', ' 999 ')!== false) {$idcard =substr ($idcard, 0,6). ' ". substr ($idcard, 6,9); }else{$idcard =substr ($idcard, 0,6). '  substr ($idcard, 6,9);  }} $idcard = $idcard. Idcard_verify_number ($idcard); return $idcard;  }//18-bit ID Verification code validity check function idcard_checksum18 ($idcard) {if (strlen ($idcard)!=18) {return false;  } $idcard _base=substr ($idcard, 0,17);  if (Idcard_verify_number ($idcard _base)!=strtoupper (substr ($idcard, 17,1)) {return false;  }else{return true; } }

Call method such as:Validation_filter_id_card (' ID card number ');

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.