common.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. function IntToChr($index, $start = 65) {
  3. $str = '';
  4. if (floor($index / 26) > 0) {
  5. $str .= IntToChr(floor($index / 26)-1);
  6. }
  7. return $str . chr($index % 26 + $start);
  8. }
  9. /********************php验证身份证号码是否正确函数*********************/
  10. function is_idcard($id)
  11. {
  12. $id = strtoupper($id);
  13. $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
  14. $arr_split = array();
  15. if(!preg_match($regx, $id))
  16. {
  17. return FALSE;
  18. }
  19. if(15==strlen($id)) //检查15位
  20. {
  21. $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
  22. @preg_match($regx, $id, $arr_split);
  23. //检查生日日期是否正确
  24. $dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
  25. if(!strtotime($dtm_birth))
  26. {
  27. return FALSE;
  28. } else {
  29. return TRUE;
  30. }
  31. }
  32. else //检查18位
  33. {
  34. $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
  35. @preg_match($regx, $id, $arr_split);
  36. $dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
  37. if(!strtotime($dtm_birth)) //检查生日日期是否正确
  38. {
  39. return FALSE;
  40. }
  41. else
  42. {
  43. //检验18位身份证的校验码是否正确。
  44. //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
  45. $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  46. $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  47. $sign = 0;
  48. for ( $i = 0; $i < 17; $i++ )
  49. {
  50. $b = (int) $id{$i};
  51. $w = $arr_int[$i];
  52. $sign += $b * $w;
  53. }
  54. $n = $sign % 11;
  55. $val_num = $arr_ch[$n];
  56. if ($val_num != substr($id,17, 1))
  57. {
  58. return FALSE;
  59. } //phpfensi.com
  60. else
  61. {
  62. return TRUE;
  63. }
  64. }
  65. }
  66. }