主页 M

php验证台湾身份证号码是否合法的代码

2016-01-21 网页编程网 网页编程网
/**
驗證台灣身份證號碼
参数:身份證號碼
返回:布爾值
用法:echo '<pre>',var_dump(Fun::twcod('n2......10')),'</pre>';
**/
public static function twcod($id) {
    $id=strtoupper($id);
    $d1=substr($id,0,1);
    if(strlen($id)!=10) {return FALSE;}
    if(stristr('ABCDEFGHJKLMNPQRSTUVXYWZIO',$d1)===FALSE) {return FALSE;}
    if(substr($id,1,1)!='1' && substr($id,1,1)!='2') {return FALSE;}
    if(!is_numeric(substr($id,1,9))) {return FALSE;}
 
    $num=array(
        'A'=>'10','B'=>'11','C'=>'12','D'=>'13','E'=>'14','F'=>'15','G'=>'16','H'=>'17','J'=>'18','K'=>'19','L'=>'20','M'=>'21','N'=>'22',
        'P'=>'23','Q'=>'24','R'=>'25','S'=>'26','T'=>'27','U'=>'28','V'=>'29','X'=>'30','Y'=>'31','W'=>'32','Z'=>'33','I'=>'34','O'=>'35',
    );
    $n1=substr($num[$d1],0,1)+(substr($num[$d1],1,1)*9);
    unset($num,$d1);
    $n2=0;
    for($j=1;$j<9;$j++) {
        $d4=substr($id,$j,1);
        $n2=$n2+$d4*(9-$j);
    }
    $n3=$n1+$n2+substr($id,9,1);
    if(($n3 % 10)!=0){return FALSE;}
    return TRUE;
}
阅读原文
阅读 4468
123 显示电脑版