Skip to content

Instantly share code, notes, and snippets.

@Foxy79
Last active February 24, 2016 15:44
Show Gist options
  • Save Foxy79/49733546ed57a180a66b to your computer and use it in GitHub Desktop.
Save Foxy79/49733546ed57a180a66b to your computer and use it in GitHub Desktop.
Luhn algorithm implementation for validate credit card number
<?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