A function _php example of verifying the correctness of ID card number in PHP

Source: Internet
Author: User
Tags strlen

ID card Verification is the simplest way to judge the length of the, of course, this very simple broken if you want to more regular we need to do some of the ID number according to the build rules to verify that some of the following functions we take a look.

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

/********************php Verify that the ID number is correct *********************/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 = "a". $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 that the birthday date is correct {return FALSE; 
      else {//verify that the 18-bit ID card is correct. 
      The check digit is generated according to ISO 7064:1983.mod 11-2, and X can be considered to be 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; 
 } 
    } 
  } 
 
}

Call ID authentication function
$IDC =is_idcard ("ID card 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) {Retu 
 RN false; 
 }//Weighted factor $factor =array (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2); 
 Check code corresponding to the value $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 card sequence code is 996 997 998 999, these are special codes for centenarians or older people 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 ID Check Code validation 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 methods such as:Validation_filter_id_card (' identity card number ');

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.