<?php | |
function is_valid_luhn($number) { | |
settype($number, 'string'); | |
$sumTable = array( | |
array(0,1,2,3,4,5,6,7,8,9), | |
array(0,2,4,6,8,1,3,5,7,9)); | |
$sum = 0; | |
$flip = 0; | |
for ($i = strlen($number) - 1; $i >= 0; $i--) { | |
$sum += $sumTable[$flip++ & 0x1][$number[$i]]; | |
} | |
return $sum % 10 === 0; | |
} |
This comment has been minimized.
This comment has been minimized.
spiro79
commented
Mar 9, 2013
Please be aware that this functions will only work if the passed argument is a string, else it will fail. It has to do with 1 thing:
Be aware of this |
This comment has been minimized.
This comment has been minimized.
gajus
commented
Jul 6, 2013
Here is a simple implementation:
|
This comment has been minimized.
This comment has been minimized.
dochenaj
commented
Jul 11, 2013
You can simply typecast whatever input you have into a string before applying it to the function or you could incorporate a step to first typecast your arguments into strings. |
This comment has been minimized.
This comment has been minimized.
liamzdenek
commented
Jun 20, 2014
This function works perfectly if your number is already in a 16-character string, But the function will fail if the string contains any symbols or spaces, This can be easily fixed by placing a preg_replace at the beginning of the function to strip all non-digit characters. $number = preg_replace("/[^0-9]/", "", $number); |
This comment has been minimized.
This comment has been minimized.
mikemirten
commented
Jun 29, 2015
PHP: function isValid($num) {
$num = preg_replace('/[^\d]/', '', $num);
$sum = '';
for ($i = strlen($num) - 1; $i >= 0; -- $i) {
$sum .= $i & 1 ? $num[$i] : $num[$i] * 2;
}
return array_sum(str_split($sum)) % 10 === 0;
} Javascript: function isValid(number) {
var num = number.replace(/[^\d]/, '');
var str = '';
for (var i = num.length - 1; i >= 0; -- i) {
str += i & 1 ? num[i] : (parseInt(num[i]) * 2).toString();
}
var sum = str.split('').reduce(function(prev, current) {
return prev + parseInt(current);
}, 0);
return sum % 10 === 0;
}; |
This comment has been minimized.
This comment has been minimized.
locoduffs
commented
Jul 17, 2015
= 0; $i--) { $sum += $sumTable[$flip++ & 0x1][$number[$i]]; } return $sum % 10 === 0; } |
This comment has been minimized.
This comment has been minimized.
ugokoli
commented
May 29, 2017
|
This comment has been minimized.
This comment has been minimized.
thomsonimjd
commented
Jul 20, 2017
is there any possibility to get bpay biller address and info by biller code? |
This comment has been minimized.
This comment has been minimized.
tflori
commented
Mar 22, 2018
•
@mikemirten first I thought thats really small and simple to understand but unfortunately it's wrong. have you tried this with a credit card number of odd length? visas can have 13 - 16 numbers and that fails. my solution is now: function validateLuhn(string $number): bool
{
$sum = '';
$revNumber = strrev($number);
$len = strlen($number);
for ($i = 0; $i < $len; $i++) {
$sum .= $i & 1 ? $revNumber[$i] * 2 : $revNumber[$i];
}
return array_sum(str_split($sum)) % 10 === 0;
} |
This comment has been minimized.
rainysia commentedAug 27, 2012