[PHP]身分證字號驗證

本範例僅適用於中華民國(台灣)的身分證

function check_personal_id($roc_id) {
    $id_head = 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);
    if (strlen($roc_id) != 10) {
        return 9;
    }
    $n0 = $id_head[strtoupper(substr($roc_id, 0, 1))];
    $n[] = '0';
    $n[] = substr($n0, 0, 1);
    $n[] = substr($n0, 1, 1);
    for ($lop1 = 1; $lop1 < strlen($roc_id); $lop1++) {
        $n[] = substr($roc_id, $lop1, 1);
    }
    return (($n[1] + ($n[2] * 9) + ($n[3] * 8) + ($n[4] * 7) + ($n[5] * 6) + ($n[6] * 5) + ($n[7] * 4) + ($n[8] * 3) + ($n[9] * 2) + $n[10] + $n[11]) % 10);
}