Heim  >  Artikel  >  Backend-Entwicklung  >  验证身份证是否合法的PHP函数

验证身份证是否合法的PHP函数

WBOY
WBOYOriginal
2016-07-25 08:42:281092Durchsuche
  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 {
  46. $b = (int) $id{$i};
  47. $w = $arr_int[$i];
  48. $sign += $b * $w;
  49. }
  50. $n = $sign % 11;
  51. $val_num = $arr_ch[$n];
  52. if ($val_num != substr($id,17, 1))
  53. {
  54. return FALSE;
  55. }
  56. else
  57. {
  58. return TRUE;
  59. }
  60. }
  61. }
  62. }
复制代码

是否合法, PHP


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn