驗證身份證是否合法的PHP函數

來源:互聯網
上載者:User
  1. /**
  2. * 身份證
  3. *
  4. * @param string $id
  5. * @return boolean
  6. */
  7. function is_idcard( $id )
  8. {
  9. $id = strtoupper($id);
  10. $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
  11. $arr_split = array();
  12. if(!preg_match($regx, $id))
  13. {
  14. return FALSE;
  15. }
  16. if(15==strlen($id)) //檢查15位
  17. {
  18. $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
  19. @preg_match($regx, $id, $arr_split);
  20. //檢查生日日期是否正確
  21. $dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
  22. if(!strtotime($dtm_birth))
  23. {
  24. return FALSE;
  25. } else {
  26. return TRUE;
  27. }
  28. }
  29. else //檢查18位
  30. {
  31. $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
  32. @preg_match($regx, $id, $arr_split);
  33. $dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
  34. if(!strtotime($dtm_birth)) //檢查生日日期是否正確
  35. {
  36. return FALSE;
  37. }
  38. else
  39. {
  40. //檢驗18位身份證的校正碼是否正確。
  41. //校正位按照ISO 7064:1983.MOD 11-2的規定產生,X可以認為是數字10。
  42. $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  43. $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  44. $sign = 0;
  45. for ( $i = 0; $i < 17; $i++ )
  46. {
  47. $b = (int) $id{$i};
  48. $w = $arr_int[$i];
  49. $sign += $b * $w;
  50. }
  51. $n = $sign % 11;
  52. $val_num = $arr_ch[$n];
  53. if ($val_num != substr($id,17, 1))
  54. {
  55. return FALSE;
  56. }
  57. else
  58. {
  59. return TRUE;
  60. }
  61. }
  62. }
  63. }
複製代碼
是否合法, PHP
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.