Wed - Jul 18, 2007 : 12:12 am
content
Luhn "Mod 10" Algorithm
Whenever I do credit card checking, I always end up searching for longer than it would take me to create it myself, so... without further ado, here's the Luhn, or "Mod 10" algorithm for validating credit card numbers.
function mod_10($cc) {
$reversed_string = strrev($cc);
$string_array = str_split($reversed_string);
$i=1;
foreach($string_array AS $character) {
if($i % 2 == 0) {
$doubled_array[] = $character * 2;
} else $doubled_array[] = $character;
$i++;
}
$doubled_string = implode("",$doubled_array);
$doubled_array2 = str_split($doubled_string);
$array_sum = array_sum($doubled_array2);
if($array_sum % 10 == 0) {
return TRUE;
} else return FALSE;
}