Skip to content

Instantly share code, notes, and snippets.

@linlong3388
Created May 27, 2015 10:01
Show Gist options
  • Save linlong3388/7945d27d3731875a433f to your computer and use it in GitHub Desktop.
Save linlong3388/7945d27d3731875a433f to your computer and use it in GitHub Desktop.
身分證字號檢查
/*****************************************************
/* 名稱 : checkNick
* 功能 : 身分證字號檢查。
****************************************************/
function checkNick($id){
//建立字母分數陣列
$head = array('A'=>1,'I'=>39,'O'=>48,'B'=>10,'C'=>19,'D'=>28,'E'=>37,'F'=>46,'G'=>55,'H'=>64,'J'=>73,'K'=>82,
'L'=>2,'M'=>11,'N'=>20,'P'=>29,'Q'=>38,'R'=>47,'S'=>56,'T'=>65,'U'=>74,'V'=>83,'W'=>21,'X'=>3,'Y'=>12,'Z'=>30,
'a'=>1,'i'=>39,'o'=>48,'b'=>10,'c'=>19,'d'=>28,'e'=>37,'f'=>46,'g'=>55,'h'=>64,'j'=>73,'k'=>82,
'l'=>2,'m'=>11,'n'=>20,'p'=>29,'q'=>38,'r'=>47,'s'=>56,'t'=>65,'u'=>74,'v'=>83,'w'=>21,'x'=>3,'y'=>12,'z'=>30
);
$multiply = array(8,7,6,5,4,3,2,1); //建立加權基數陣列
//檢查身份字格式是否正確
if (preg_match("/^[a-zA-Z][1-2][0-9]+$/",$id) && strlen($id) == 10){
$len = strlen($id); //切開字串
for($i=0; $i<$len; $i++){
$stringArray[$i] = substr($id,$i,1);
}
$total = $head[array_shift($stringArray)]; //取得字母分數
$point = array_pop($stringArray); //取得比對碼
$len = count($stringArray); //取得數字分數
for($j=0; $j<$len; $j++){
$total += $stringArray[$j]*$multiply[$j];
}
if (($total%10 == 0 )?0:10-$total%10 != $point) { //檢查比對碼
return false;
} else {
return true;
}
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment