Php does not use regular expressions to verify true and false ID card _ PHP Tutorial

Source: Internet
Author: User
Php does not need regular expressions to verify true and false ID cards. Let's look at the code directly. a simple PHP class copies the code as follows :? Php $ IDCardnewIDCard (); var_dump ($ IDCard: isCard ($ _ GET [card]); you can directly view the code for the *** ID card processing class ** classIDC, A simple PHP class

The code is as follows:


$ IDCard = new IDCard ();
Var_dump ($ IDCard: isCard ($ _ GET ['card ']);

/**
* Id card processing
*/
Class IDCard {

// Check whether the ID card is correct
Public static function isCard ($ card ){
$ Card = self: to18Card ($ card );
If (strlen ($ card )! = 18 ){
Return false;
}

$ CardBase = substr ($ card, 0, 17 );

Return (self: getVerifyNum ($ cardBase) = strtoupper (substr ($ card, 17, 1 )));
}


// Format the 15-digit ID card number to 18
Public static function to18Card ($ card ){
$ Card = trim ($ card );

If (strlen ($ card) = 18 ){
Return $ card;
}

If (strlen ($ card )! = 15 ){
Return false;
}

// If the ID card sequence code is 996 997 998 999, these are special codes for elderly people over years old
If (array_search (substr ($ card, 12, 3), array ('20160301', '20160301', '20160301 '))! = False ){
$ Card = substr ($ card, 0, 6). '18'. substr ($ card, 6, 9 );
} Else {
$ Card = substr ($ card, 0, 6). '19'. substr ($ card, 6, 9 );
}
$ Card = $ card. self: getVerifyNum ($ card );
Return $ card;
}

// Calculate the ID card verification code based on the national standard gb 11643-1999
Private static function getVerifyNum ($ cardBase ){
If (strlen ($ cardBase )! = 17 ){
Return false;
}
// Weighting factor
$ Factor = array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 );

// Check code value
$ Verify_number_list = array ('1', '0', 'X', '9', '8', '7', '6', '5 ', '4', '3', '2 ');

$ Checksum = 0;
For ($ I = 0; $ I <strlen ($ cardBase); $ I ++ ){
$ Checksum + = substr ($ cardBase, $ I, 1) * $ factor [$ I];
}

$ Mod = $ checksum % 11;
$ Verify_number = $ verify_number_list [$ mod];

Return $ verify_number;
}
}


?>

The pipeline code is as follows :? Php $ IDCard = new IDCard (); var_dump ($ IDCard: isCard ($ _ GET ['card ']); /*** ID card processing class */class IDC...

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.