Tag: PHP

参考

番号チェック

<?php

$res = checkCardNum('5457623898234113');
var_dump($res);

function checkCardNum($num)
{
    $rev = strrev($num);
    $total = 0;
    for ($i = 1; $i < strlen($rev); $i++) {
        $n = 0;
        switch ($i % 2) {
        case 1:
            $n += $rev[$i];
        case 0:
            $n += $rev[$i];
        }
        $total += $n >= 10 ? $n % 10 + 1 : $n;
    }
    $checkDigit = 10 - ($total % 10);
    return $checkDigit == $rev[0];
}

Last-modified: 2013-07-26 (金) 20:10:40 (1489d)