身份证校验函数

来源:互联网 发布:博客网站源码 编辑:程序博客网 时间:2024/04/29 10:05
 function checkidcard($idcard)
{
    $idcard_len=strlen($idcard);
    if($idcard_len!='15'&&$idcard_len!='18')
    {
        echo '身份证位数不正确';
    }
    elseif(!eregi("^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)+([0-9]{4})+(19|20)+([0-9]{2})+(0)+([1-9]{1})+(0)+([1-9]{1})+([0-9]{3})+([0-9xy]{1})$",$idcard)
    //1999/2099年09月09日    18位
    &&!eregi("^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)+([0-9]{4})+(19|20)+([0-9]{2})+(0)+([1-9]{1})+(1|2)+([0-9]{1})+([0-9]{3})+([0-9xy]{1})$",$idcard)
    //1999/2099年09月29日    18位
    &&!eregi("^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)+([0-9]{4})+(19|20)+([0-9]{2})+(0)+([1-9]{1})+(3)+([01]{1})+([0-9]{3})+([0-9xy]{1})$",$idcard)
    //1999/2099年09月31日    18位
    &&!eregi("^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)+([0-9]{4})+(19|20)+([0-9]{2})+(1)+([012]{1})+(0)+([1-9]{1})+([0-9]{3})+([0-9xy]{1})$",$idcard)
    //1999/2099年12月09日    18位
    &&!eregi("^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)+([0-9]{4})+(19|20)+([0-9]{2})+(1)+([012]{1})+(1|2)+([0-9]{1})+([0-9]{3})+([0-9xy]{1})$",$idcard)
    //1999/2099年12月29日    18位
    &&!eregi("^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)+([0-9]{4})+(19|20)+([0-9]{2})+(1)+([012]{1})+(3)+([01]{1})+([0-9]{3})+([0-9xy]{1})$",$idcard)
    //1999/2099年12月31日    18位
    &&!ereg("^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)+([0-9]{4})+([0-9]{2})+(0)+([1-9]{1})+(0)+([1-9]{1})+([0-9]{3})$",$idcard)
    //99年09月09日        15位
    &&!ereg("^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)+([0-9]{4})+([0-9]{2})+(0)+([1-9]{1})+(1|2)+([0-9]{1})+([0-9]{3})$",$idcard)
    //99年09月29日        15位
    &&!ereg("^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)+([0-9]{4})+([0-9]{2})+(0)+([1-9]{1})+(3)+(0|1)+([0-9]{3})$",$idcard)
    //99年09月31日        15位
    &&!ereg("^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)+([0-9]{4})+([0-9]{2})+(1)+(0|1|2)+(0)+([1-9]{1})+([0-9]{3})$",$idcard)
    //99年12月09日        15位
    &&!ereg("^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)+([0-9]{4})+([0-9]{2})+(1)+(0|1|2)+(1|2)+([0-9]{1})+([0-9]{3})$",$idcard)
    //99年12月29日        15位
    &&!ereg("^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)+([0-9]{4})+([0-9]{2})+(1)+(0|1|2)+(3)+(0|1)+([0-9]{3})$",$idcard))
    //99年12月31日        15位
    {
        echo '身份证格式不符合规定要求';
    }
    elseif(strlen($idcard)=='15')
    {
        $year='19'.substr($idcard,6,2);        //现在应该没有2000年以后的15位身份证了吧?(:
        $year=='1900'?$div_exa=$year%400:$div_exa=$year%4;        //求闰年,如果为世纪年则用400作为除数
        if($div_exa=='1')
        {
            if(substr($idcard,8,2)=='02'&&substr($idcard,10,2)>'28')    //非闰年
            {
                echo '身份证格式错误,非闰年二月份只有28天';
            }
        }
        elseif($div_exa=='0')
        {
            if(substr($idcard,8,2)=='02'&&substr($idcard,10,2)>'29')    //闰年
            {
                echo '身份证格式错误,闰年的二月份只有29天';
            }
        }
        else
        {
            $message='通过身份证格式检测';        //初始化$message的值
            switch(substr($idcard,8,2))
            {
                case 04:
                    if(substr($idcard,10,2)>'30')
                    {
                        $message= '身份证格式错误,四月份没有31号';
                    }
                break;
                case 06:
                    if(substr($idcard,10,2)>'30')
                    {
                        $message= '身份证格式错误,六月份没有31号';
                    }
                break;
                case 09:
                    if(substr($idcard,10,2)>'30')
                    {
                        $message= '身份证格式错误,九月份没有31号';
                    }
                break;
                case 11:
                    if(substr($idcard,10,2)>'30')
                    {
                        $message= '身份证格式错误,十一月份没有31号';
                    }
                break;
                default;
            }
            echo $message;
        }
    }
    elseif(strlen($idcard)=='18')
    {
        $year=substr($idcard,6,4);
        $year=='2000'?$div_exa=$year%400:$div_exa=$year%4;        //求闰年,如果为世纪年则用400作为除数
        if($div_exa=='1')
        {
            if(substr($idcard,10,2)=='02'&&substr($idcard,12,2)>'28')    //非闰年
            {
                echo '身份证格式错误,非闰年二月份只有28天';
            }
        }
        elseif($div_exa=='0')
        {
            if(substr($idcard,10,2)=='02'&&substr($idcard,12,2)>'29')    //闰年
            {
                echo '身份证格式错误,闰年的二月份只有29天';
            }
        }
        else
        {
            $message='通过身份证格式检测';        //初始化$message的值
            switch(substr($idcard,10,2))
            {
                case 04:
                    if(substr($idcard,12,2)>'30')
                    {
                        $message= '身份证格式错误,四月份没有31号';
                    }
                break;
                case 06:
                    if(substr($idcard,12,2)>'30')
                    {
                        $message= '身份证格式错误,六月份没有31号';
                    }
                break;
                case 09:
                    if(substr($idcard,12,2)>'30')
                    {
                        $message= '身份证格式错误,九月份没有31号';
                    }
                break;
                case 11:
                    if(substr($idcard,12,2)>'30')
                    {
                        $message= '身份证格式错误,十一月份没有31号';
                    }
                break;
                default;
            }
            echo $message;
        }
    }
}
原创粉丝点击