Function for verifying the ID card number in PHP

Source: Internet
Author: User
Function for verifying the ID card number in PHP

  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 based on 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 (, 2 );
  18. // Check code value
  19. $ Verify_number_list = array ('1', '0', 'X', '9', '8', '7', '6', '5 ', '4', '3', '2 ');
  20. $ Checksum = 0;
  21. For ($ I = 0; $ I $ Checksum + = substr ($ idcard_base, $ I, 1) * $ factor [$ I];
  22. }
  23. $ Mod = $ checksum % 11;
  24. $ Verify_number = $ verify_number_list [$ mod];
  25. Return $ verify_number;
  26. }
  27. // Upgrade the 15-digit ID card to 18-digit
  28. Function idcard_15to18 ($ idcard ){
  29. If (strlen ($ idcard )! = 15 ){
  30. Return false;
  31. } Else {
  32. // If the ID card sequence code is 996 997 998 999, these are special codes for elderly people over years old
  33. If (array_search (substr ($ idcard, 996), array ('20140901', '20160901', '20160301 '))! = False ){
  34. $ Idcard = substr ($ idcard,). '18'. substr ($ idcard, 6, 9 );
  35. } Else {
  36. $ Idcard = substr ($ idcard,). '19'. substr ($ idcard, 6, 9 );
  37. }
  38. }
  39. $ Idcard = $ idcard. idcard_verify_number ($ idcard );
  40. Return $ idcard;
  41. }
  42. // Check the validity of the 18-digit ID card verification code
  43. Function idcard_checksum18 ($ idcard ){
  44. If (strlen ($ idcard )! = 18 ){
  45. Return false;
  46. }
  47. $ Idcard_base = substr ($ idcard, 0, 17 );
  48. If (idcard_verify_number ($ idcard_base )! = Strtoupper (substr ($ idcard, 17,1 ))){
  49. Return false;
  50. } Else {
  51. Return true;
  52. }
  53. }

Call method:

  1. Validation_filter_id_card ('ID card number ');

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.