PHP detection ID card number two functions _ PHP Tutorial

Source: Internet
Author: User
PHP checks the ID number of two functions. Here we have introduced that the ID card number must be a simple one and cannot judge whether the ID card number is legal or exists. it only meets some standards, let's take a look at the two instances. here we have introduced that the ID card number must be a simple one that does not determine whether the ID card number is legal or does not exist. it only meets some standards. let's look at the two instances below.

The most accurate way to check the ID card number is through the national identity data center. I don't know if I want to know it. this is not so good. so I will introduce a method as follows, the first 17 bits are used for detection. The following is an example:

The code is as follows:

$ IdCard = '000000'; // The first 17 digits of the ID card number
$ Wi = array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 );
$ Ai = array ('1', '0', 'X', '9', '8', '7', '6', '5 ', '4', '3', '2 ');
$ Sigma = null;
For ($ I = 0; $ I <17; $ I ++ ){
$ Sigma + = (int) $ idCard {$ I}) * $ wi [$ I];
}
Echo "ID number:". $ idCard. $ ai [($ sigma % 11)];
?>

Example 2

The code is as follows:


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 the ID card verification code based on the national standard GB 11643-1999
Function idcard_verify_number ($ idcard_base)
{
If (strlen ($ idcard_base )! = 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 ($ idcard_base); $ I ++)
{
$ Checksum + = substr ($ idcard_base, $ I, 1) * $ factor [$ I];
}
$ Mod = $ checksum % 11;
$ Verify_number = $ verify_number_list [$ mod];
Return $ verify_number;
}
// Upgrade the 15-digit ID card to 18-digit
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 elderly people over years old
If (array_search (substr ($ idcard, 12, 3), array ('20160301', '20160301', '20160301 '))! = False ){
$ Idcard = substr ($ idcard, 0, 6). '18'. substr ($ idcard, 6, 9 );
} Else {
$ Idcard = substr ($ idcard, 0, 6). '19'. substr ($ idcard, 6, 9 );
}
}
$ Idcard = $ idcard. idcard_verify_number ($ idcard );
Return $ idcard;
}
// Check the validity of the 18-digit ID card verification code
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;
}
}

...

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.