I wrote several ID card functions, and I personally felt very useful. Especially in the online questionnaire survey, it is very useful to verify the ID card in my personal information, but there are few applicants, you can leave it in your own blog.
// 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 );