Generally in the use of the time without direct call Idcard_verify_number (), most of the usual applications are used after two functions, these functions are not concerned about the format of identity card strings, before the call to check their own format.
? Php
Calculate ID Check code, according to national standard GB 11643-1999
function Idcard_verify_number ($idcard _base) {
if (strlen ($idcard _base)!= {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 IDs 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 and older people
if (Array_search ($idcard, 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 card Verification code validity check
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;
}
}
? >
$idcard_base refers to the identity card in the standard code, the standard code is 18 ID card, that is, 18 ID card before the first 17, the last one called Parity code
Generally in the use of the time without direct call Idcard_verify_number (), most of the usual applications are used after two functions, these functions are not concerned about the format of identity card strings, before the call to check their own format.