PHP function to verify ID number

WBOY
Release: 2016-07-25 08:55:02
Original
1201 people have browsed it
  1. function validation_filter_id_card($id_card){
  2. if(strlen($id_card)==18){
  3. return idcard_checksum18($id_card);
  4. }elseif((strlen($id_card)==15)){
  5. $id_card=idcard_15to18($id_card);
  6. return idcard_checksum18($id_card);
  7. }else{
  8. return false;
  9. }
  10. }
  11. // Calculate the ID card verification code, according to the national standard GB 11643-1999
  12. function idcard_verify_number( $idcard_base){
  13. if(strlen($idcard_base)!=17){
  14. return false;
  15. }
  16. //Weighting factor
  17. $factor=array(7,9,10,5,8,4,2,1, 6,3,7,9,10,5,8,4,2);
  18. //Corresponding value of the check code
  19. $verify_number_list=array('1','0','X','9',' 8','7','6','5','4','3','2');
  20. $checksum=0;
  21. for($i=0;$i
  22. $checksum += substr($idcard_base,$i,1) * $factor[$i];
  23. }
  24. $mod=$checksum % 11;
  25. $verify_number=$verify_number_list[$mod];
  26. return $verify_number;
  27. }
  28. // Upgrade the 15-digit ID card to 18 digits
  29. function idcard_15to18($idcard){
  30. if(strlen($idcard)!=15){
  31. return false;
  32. }else{
  33. / / If the ID card sequence code is 996 997 998 999, these are special codes for people over 100 years old
  34. if(array_search(substr($idcard,12,3),array('996','997','998' ,'999')) !== false){
  35. $idcard=substr($idcard,0,6).'18'.substr($idcard,6,9);
  36. }else{
  37. $idcard=substr( $idcard,0,6).'19'.substr($idcard,6,9);
  38. }
  39. }
  40. $idcard=$idcard.idcard_verify_number($idcard);
  41. return $idcard;
  42. }
  43. // 18 Validity check of ID card verification code
  44. function idcard_checksum18($idcard){
  45. if(strlen($idcard)!=18){
  46. return false;
  47. }
  48. $idcard_base=substr($idcard,0,17);
  49. if(idcard_verify_number($idcard_base)!=strtoupper(substr($idcard,17,1))){
  50. return false;
  51. }else{
  52. return true;
  53. }
  54. }
Copy code

Call method :

  1. validation_filter_id_card('ID number');
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!