Last active
February 24, 2016 15:44
-
-
Save Foxy79/49733546ed57a180a66b to your computer and use it in GitHub Desktop.
Luhn algorithm implementation for validate credit card number
This file contains hidden or 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
<?php | |
/* | |
-(bool) luhnValidate ("number"); | |
*/ | |
function luhnValidate($string) { | |
$string = preg_replace("![^0-9]!","",$string); | |
$result = ""; | |
foreach (str_split(strrev((string) $string)) as $i => $d) { $result .= $i %2 !== 0 ? $d * 2 : $d; } | |
return array_sum(str_split($result)) % 10 === 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment