According to the algorithm of the payment card check digit in ISO 2894 the Luhn Mod-10 method stipulates:
1. Multiply each digit on the card number by the weight. The rule is that if the number of card number is even, then the first bit is multiplied by 2, otherwise it is multiplied by 1, then respectively, 1,2,1,2,1,2;
2, if each digit multiplied by the weight after more than 9, you need to subtract 9;
3. Sum all the weighted numbers processed, and use the number 10 to calculate the modulus;
4, the remainder should be 0, otherwise it may be an input error. It could be a fake number.
Handy PHP Simple implementation, the actual scene front-end verification better, such as JS.
Copy the Code code as follows:
function Check_card ($card) {
if (!is_numeric ($card)) return False;
$card _len = strlen ($card);
$i = 0;
$num _i = Array ();
do{
if (! $i) {
$num _x = $card _len% 2? 1:2;
} else {
$num _x = $num _x = = 1? 2:1;
}
$num _i[$i] = (int) $card [$i] * $num _x;
$num _i[$i] = $num _i[$i] > 9? $num _i[$i]-9: $num _i[$i];
}while (Isset ($card [+ + $i]));
$num _sum = array_sum ($num _i);
Return $num _sum% 10? False:true;
}