PHP detection ID Number two functions _php tutorial

Source: Internet
Author: User
We introduce the ID card number is definitely a simple can not judge the identity card number is not legal or exist, just meet some criteria on it, see below two examples.

Detection of identity card number, the most accurate affirmation is through the National Identity Data center detection, think and do not know, this thing is not so good to get, so the following describes a method, through the first 17 bits to detect, directly on the example:

The code is as follows Copy Code

$idCard = ' 12345678901234567 ';//ID number first 17 digits
$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 < $i + +) {
$sigma + = ((int) $idCard {$i}) * $wi [$i];
}
echo "ID number:". $idCard. $ai [($sigma% 11)];
?>

Example 2

The code is as follows Copy Code


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 false;
}
Weighting factor
$factor = Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
Check code corresponding 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 sequence code is 996 997 998 999, these are special codes for centenarians older than
if (Array_search ($idcard, substr, 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;
}
Verification code validity check for 18-bit ID card
function Idcard_checksum18 ($idcard) {
if (strlen ($idcard)! =) {return false;}
$idcard _base = substr ($idcard, 0, 17);
if (Idcard_verify_number ($idcard _base)! = Strtoupper (substr ($idcard, 17, 1))) {
return false;
}else{
return true;
}
}

http://www.bkjia.com/PHPjc/632756.html www.bkjia.com true http://www.bkjia.com/PHPjc/632756.html techarticle We introduce the ID card number is definitely a simple can not judge the identity card number is not legal or exist, just meet some criteria on it, see below two examples ...

  • 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.