国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP實現可精確驗證身份證號碼的工具類示例

2024-05-05 00:04:01
字體:
來源:轉載
供稿:網友

本文實例講述了PHP實現可精確驗證身份證號碼的工具類。分享給大家供大家參考,具體如下:

<?phpclass check_IdCard { // $num為身份證號碼,$checkSex:1為男,2為女,不輸入為不驗證 public function checkIdentity($num, $checkSex = '') { // 不是15位或不是18位都是無效身份證號  if (strlen($num) != 15 && strlen($num) != 18) {   return false;  }  // 是數值  if (is_numeric($num)) {   // 如果是15位身份證號   if (strlen($num) == 15) {    // 省市縣(6位)    $areaNum = substr($num, 0, 6);    // 出生年月(6位)    $dateNum = substr($num, 6, 6);    // 性別(3位)    $sexNum = substr($num, 12, 3);   } else {    // 如果是18位身份證號    // 省市縣(6位)    $areaNum = substr($num, 0, 6);    // 出生年月(8位)    $dateNum = substr($num, 6, 8);    // 性別(3位)    $sexNum = substr($num, 14, 3);    // 校驗碼(1位)    $endNum = substr($num, 17, 1);   }  } else {   // 不是數值   if (strlen($num) == 15) {    return false;   } else {    //驗證前17位為數值,且18位為字符x    $check17 = substr($num, 0, 17);    if (!is_numeric($check17)) {     return false;    }    //省市縣(6位)    $areaNum = substr($num, 0, 6);    // 出生年月(8位)    $dateNum = substr($num, 6, 8);    // 性別(3位)    $sexNum = substr($num, 14, 3);    // 校驗碼(1位)    $endNum = substr($num, 17, 1);    if ($endNum != 'x' && $endNum != 'X') {     return false;    }   }  }  //驗證地區  if (isset($areaNum)) {   if (!$this->checkArea($areaNum)) {    return false;   }  }  //驗證日期  if (isset($dateNum)) {   if (!$this->checkDate($dateNum)) {    return false;   }  }  // 性別1為男,2為女  if ($checkSex == 1) {   if (isset($sexNum)) {    if (!$this->checkSex($sexNum)) {     return false;    }   }  } elseif ($checkSex == 2) {   if (isset($sexNum)) {    if ($this->checkSex($sexNum)) {     return false;    }   }  }  //驗證最后一位  if (isset($endNum)) {   if (!$this->checkEnd($endNum, $num)) {    return false;   }  }  return true; } // 驗證城市 private function checkArea($area) {  $num1 = substr($area, 0, 2);  $num2 = substr($area, 2, 2);  $num3 = substr($area, 4, 2);  // 根據GB/T2260—999,省市代碼11到65  if (10 < $num1 && $num1 < 66) {   return true;  } else {   return false;  } } // 驗證出生日期 private function checkDate($date) {  if (strlen($date) == 6) {   $date1 = substr($date, 0, 2);   $date2 = substr($date, 2, 2);   $date3 = substr($date, 4, 2);   $statusY = $this->checkY('19' . $date1);  } else {   $date1 = substr($date, 0, 4);   $date2 = substr($date, 4, 2);   $date3 = substr($date, 6, 2);   $nowY = date("Y", time());   if (1900 < $date1 && $date1 <= $nowY) {    $statusY = $this->checkY($date1);   } else {    return false;   }  }  if (0 < $date2 && $date2 < 13) {   if ($date2 == 2) {    // 潤年    if ($statusY) {     if (0 < $date3 && $date3 <= 29) {      return true;     } else {      return false;     }    } else {     // 平年     if (0 < $date3 && $date3 <= 28) {      return true;     } else {      return false;     }    }   } else {    $maxDateNum = $this->getDateNum($date2);    if (0 < $date3 && $date3 <= $maxDateNum) {     return true;    } else {     return false;    }   }  } else {   return false;  } } // 驗證性別 private function checkSex($sex) {  if ($sex % 2 == 0) {   return false;  } else {   return true;  } } // 驗證18位身份證最后一位 private function checkEnd($end, $num) {  $checkHou = array(1, 0, 'x', 9, 8, 7, 6, 5, 4, 3, 2);  $checkGu = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);  $sum = 0;  for ($i = 0;$i < 17;$i++) {   $sum+= (int)$checkGu[$i] * (int)$num[$i];  }  $checkHouParameter = $sum % 11;  if ($checkHou[$checkHouParameter] != $num[17]) {   return false;  } else {   return true;  } } // 驗證平年潤年,參數年份,返回 true為潤年 false為平年 private function checkY($Y) {  if (getType($Y) == 'string') {   $Y = (int)$Y;  }  if ($Y % 100 == 0) {   if ($Y % 400 == 0) {    return true;   } else {    return false;   }  } else if ($Y % 4 == 0) {   return true;  } else {   return false;  } } // 當月天數 參數月份(不包括2月) 返回天數 private function getDateNum($month) {  if ($month == 1 || $month == 3 || $month == 5 || $month == 7 || $month == 8 || $month == 10 || $month == 12) {   return 31;  } else if ($month == 2) {  } else {   return 30;  } }}// 測試header("content-type:text/html;charset=utf-8");$num = '230106199202099035'; //此號碼為隨機生成$test = new check_IdCard();$data = $test->checkIdentity($num);var_dump($data);//=============新的18位身份證號碼各位的含義:=======================//1-2位省、自治區、直轄市代碼;11-65//3-4位地級市、盟、自治州代碼;//5-6位縣、縣級市、區代碼;//7-14位出生年月日,比如19670401代表1967年4月1日;//15-17位為順序號,其中17位男為單數,女為雙數;//18位為校驗碼,0-9和X,由公式隨機產生。//舉例://130503 19670401 0012這個身份證號的含義: 13為河北,05為邢臺,03為橋西區,出生日期為1967年4月1日,順序號為001,2為驗證碼//===========15位身份證號碼各位的含義:=======================//1-2位省、自治區、直轄市代碼;//3-4位地級市、盟、自治州代碼;//5-6位縣、縣級市、區代碼;//7-12位出生年月日,比如670401代表1967年4月1日,這是和18位號碼的第一個區別;//13-15位為順序號,其中15位男為單數,女為雙數;//與18位身份證號的第二個區別:沒有最后一位的驗證碼。//舉例://130503 670401 001的含義; 13為河北,05為邢臺,03為橋西區,出生日期為1967年4月1日,順序號為001。

運行結果:

bool(true)

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 丰镇市| 丹东市| 黎平县| 昂仁县| 拜城县| 达日县| 阜平县| 左云县| 隆回县| 新巴尔虎左旗| 额尔古纳市| 江安县| 乌鲁木齐市| 罗江县| 炉霍县| 株洲市| 原阳县| 石城县| 雷州市| 木里| 四川省| 信丰县| 万源市| 元朗区| 玉山县| 澜沧| 马鞍山市| 铁力市| 永新县| 东方市| 宝清县| 新邵县| 新闻| 池州市| 烟台市| 盐亭县| 榆中县| 长乐市| 龙泉市| 镇安县| 抚顺市|