PHP實現(xiàn)可精確驗證身份證號碼的工具類示例
本文實例講述了PHP實現(xiàn)可精確驗證身份證號碼的工具類。分享給大家供大家參考,具體如下:
<?php
class check_IdCard {
// $num為身份證號碼,$checkSex:1為男,2為女,不輸入為不驗證
public function checkIdentity($num, $checkSex = '') { // 不是15位或不是18位都是無效身份證號
if (strlen($num) != 15 && strlen($num) != 18) {
return false;
}
// 是數(shù)值
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 {
// 不是數(shù)值
if (strlen($num) == 15) {
return false;
} else {
//驗證前17位為數(shù)值,且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;
}
}
}
//驗證地區(qū)
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);
// 根據(jù)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;
}
}
// 驗證平年潤年,參數(shù)年份,返回 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;
}
}
// 當月天數(shù) 參數(shù)月份(不包括2月) 返回天數(shù)
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位省、自治區(qū)、直轄市代碼;11-65
//3-4位地級市、盟、自治州代碼;
//5-6位縣、縣級市、區(qū)代碼;
//7-14位出生年月日,比如19670401代表1967年4月1日;
//15-17位為順序號,其中17位男為單數(shù),女為雙數(shù);
//18位為校驗碼,0-9和X,由公式隨機產(chǎn)生。
//舉例:
//130503 19670401 0012這個身份證號的含義: 13為河北,05為邢臺,03為橋西區(qū),出生日期為1967年4月1日,順序號為001,2為驗證碼
//===========15位身份證號碼各位的含義:=======================
//1-2位省、自治區(qū)、直轄市代碼;
//3-4位地級市、盟、自治州代碼;
//5-6位縣、縣級市、區(qū)代碼;
//7-12位出生年月日,比如670401代表1967年4月1日,這是和18位號碼的第一個區(qū)別;
//13-15位為順序號,其中15位男為單數(shù),女為雙數(shù);
//與18位身份證號的第二個區(qū)別:沒有最后一位的驗證碼。
//舉例:
//130503 670401 001的含義; 13為河北,05為邢臺,03為橋西區(qū),出生日期為1967年4月1日,順序號為001。
運行結果:
bool(true)
PS:這里再為大家推薦一款相似的在線工具供大家參考:
身份證歸屬地信息在線查詢:
http://tools.jb51.net/bianmin/sfz
另外,本站在線工具小程序上也有一款功能更加強大的身份證信息獲取工具,感興趣的朋友可以掃描如下小程序碼查看:

更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)學運算技巧總結》、《php字符串(string)用法總結》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結構與算法教程》、《php程序設計算法總結》及《php正則表達式用法總結》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
php+js實現(xiàn)的拖動滑塊驗證碼驗證表單操作示例【附源碼下載】
這篇文章主要介紹了php+js實現(xiàn)的拖動滑塊驗證碼驗證表單操作,結合實例形式分析了php+js拖動滑塊驗證碼驗證表單操作基本功能實現(xiàn)與使用相關操作技巧,需要的朋友可以參考下2020-05-05
使用PHPMailer實現(xiàn)郵件的實時發(fā)送功能
這篇文章主要為大家詳細介紹了如何使用PHPMailer 實現(xiàn)一個接收詢盤并實時同步到指定郵箱的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-12-12
phpmailer發(fā)送郵件之后,返回收件人是否閱讀了郵件的方法
這篇文章主要介紹了phpmailer發(fā)送郵件之后,返回收件人是否閱讀了郵件的方法,該功能非常簡單實用,需要的朋友可以參考下2014-07-07
PHP Warning: Module ''modulename'' already loaded in問題解決辦法
這篇文章主要介紹了PHP Warning: Module 'modulename' already loaded in問題解決辦法,本文總結了兩種情況,需要的朋友可以參考下2015-03-03

