-
Notifications
You must be signed in to change notification settings - Fork 0
/
luhn.php
59 lines (50 loc) · 1.66 KB
/
luhn.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/*
* Check if a given number is a valid Luhn number, and if the control digit is correct
*
* Asumes the input to be digits only, and last one to be the control digit.
* Only zeros are considered valid, and should be taken care or outside of this function.
*
* @see http://en.wikipedia.org/wiki/Luhn_algorithm
*/
public function isLuhnNumber($digits = NULL) {
if (is_numeric($digits)) {
// Calculate the digits with start from the last (which is the check digit)
$digits = strrev($digits);
$checksum = 0;
$multiplier = 1;
// for each other digit, multiply with 1 and 2
// If the sum is greater than 9, substract 9
// Add sum to the total checksum
for ($index = 0; $index < strlen($digits); $index++) {
$sum = $digits[$index] * $multiplier;
if ($sum > 9) {
$checksum += $sum - 9;
} else {
$checksum += $sum;
}
if ($multiplier == 1) {
$multiplier = 2;
} else {
$multiplier = 1;
}
}
// If the checksum modulus 10 is 0, the checksum is correct
if (($checksum % 10) === 0) {
// Next check that the supplied control digit is correct
// The last digit is the control digit, substract its value from the checksum
// Since the number is reversed, this is the first digit
$controldigit = $digits[0];
$checksum -= $controldigit;
// Get next even 10-multiplier
$next10multiplier = ceil($checksum / 10) * 10;
// Verify that the given check digit is in fact correct
// Substract the checksum (without value for control digit) from the next even 10 multiplier
if (($next10multiplier - $checksum) == $controldigit) {
return true;
}
}
}
return false;
}
?>