According to the algorithm of the payment card check bit in ISO 2894, the Luhn Mod-10 method stipulates:
1, on the card number per digit multiplied by the weight. The rule is that if the number of card numbers is even, the first is multiplied by 2, or multiplied by 1, and then, respectively, 1,2,1,2,1,2;
2, if each digit times the weight after more than 9, then need to subtract 9;
3, all the processed weighted numbers sum, with the number 10 modulo operation;
4, the remainder should be 0, otherwise it may be an input error. It could also be a fake number.
Conveniently PHP simple implementation, the actual scene front-end verification is better, such as JS.
Copy 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;
}