Last active
June 22, 2018 10:13
-
-
Save stackoverflows/f60bdfe34123ca3e187007732204f017 to your computer and use it in GitHub Desktop.
Working PHP Credit Card luhn check (preliminary credit card number validation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// will update with suggestions | |
function luhnCheck($number): bool | |
{ | |
$number = preg_replace("/[^0-9]/", "", strval($number)); // convert to string and remove non numeric characters | |
$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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment