PHP身份證號驗證

   /**this

     * Check per pin編碼

     *.net

     * @param   string  $pinget

     * @return  arraystring

     */io

public function checkPerPin($pin)function

{class

    $length = strlen($pin);升級


    if ($length == 15) {return

        $var ['pin'] = $this->IdCard15To18($pin);


    }


    if ($length == 18) {


        if (! $this->IdcardCheckSum18($pin)) {


            return false;

        }

    }


    if ($length != 18 && $length != 15) {


        return false;

    }


    return true;

}



/**

 * 將15位號碼升至18位

 *

 * @param String $idcard

 * @return String

 */

function IdCard15To18($idcard) // 功能:將15位身份證升級到18位

{


    if (strlen($idcard ) != 15){


        return false;

    } else {


        // 若是身份證順序碼是996 997 998 999,這些是爲百歲以上老人的特殊編碼

        if (array_search ( substr ( $idcard, 12, 3 ), array ('996', '997', '998', '999' ) ) !== false) {

            $idcard = substr ( $idcard, 0, 6 ) . '18' . substr ( $idcard, 6, 9 );

        } else {

            $idcard = substr ( $idcard, 0, 6 ) . '19' . substr ( $idcard, 6, 9 );

        }

    }


    $idcard = $idcard . ($this->IdcardVerifyNumber ( $idcard ));


    return $idcard;

}



/**

 * 18位身份證號碼的有效性檢查

 *

 * @param String $idcard

 * @return Boolean

 */

function IdcardCheckSum18($idcard)

{

    if (strlen ( $idcard ) != 18) {


        return false;

    }

    $idcard_base = substr ( $idcard, 0, 17 );


    if (($this->IdcardVerifyNumber ( $idcard_base )) != strtoupper ( substr ( $idcard, 17, 1 ) )) {


        return false;

    } else {


        return true;

    }

}



/**

 * 計算身份證校驗碼

 *

 * @param String $idcard_base

 * @return String

 */

function IdcardVerifyNumber($idcard_base) // 功能:計算身份證校驗碼,根據國家標準GB 11643-1999

{

    if (strlen ( $idcard_base ) != 17)

    {

        return false;

    }


    $factor = array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 );


    $verify_number_list = array ('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' );


    $checksum = 0;

    for($i = 0; $i < strlen ( $idcard_base ); $i ++)

    {

        $checksum += substr ( $idcard_base, $i, 1 ) * $factor [$i];

    }


    $mod = $checksum % 11;

    $verify_number = $verify_number_list [$mod];


    return $verify_number;


}

相關文章
相關標籤/搜索