自從有信用卡後...就常在想怎麼驗證信用卡號..
今天總算看到了...特別摘錄一下
參考資料來源:PHP 檢查信用卡號碼
PHP: 
/* luhn_checker(): This is a small PHP function for checking valid *
* credit card with LUHN algorithm                                  *
*                                                                  *
* Last updated: 26 August 2007                                     *
* This is a free PHP script under GNU GPL version 2.0 or above     *
* Copyright (C) 2007 Sam Tang                                      *
* Feedback/comment/suggestions : http://www.real-blog.com/        */
 
function luhn_checker($card_num){
    // 將非數字的字串移除
    $card_num = preg_replace("/\D|\s/", "", $card_num);
 
    $sum = 0;
    for($i=0; $i
        if(($i % 2) == 0){
            // 在單數位置的數值乘 2
            $digit = $digit * 2;
        }
 
        if ($digit> 9)  $digit = $digit - 9;
        $sum += $digit;
    }
 
    if(($sum % 10) == 0 && strlen($card_num) == 16){
        return TRUE;
    }else{
        return FALSE;
    }
}
 
/* Example
if(luhn_checker("1234567812345678")){
    echo "Correct!";
}else{
    echo "Wrong card number!";
}
*/
?> 
沒有留言:
張貼留言