Last active
May 22, 2016 16:59
-
-
Save jankal/49407140c085f9efff01 to your computer and use it in GitHub Desktop.
A PHP class for checking numbers trough the popular Luhn algorithm. It's also able to get the digit which you'll have to add that then Lun-check gets true.
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
-----BEGIN PGP SIGNED MESSAGE----- | |
Hash: SHA256 | |
<?php | |
class Luhn { | |
public static function getDigit($number) { | |
$number .= 0; | |
$result = self::calc($number); | |
return (10 - $result); | |
} | |
public static function calc($number) { | |
$sum = 0; | |
$numDigits = strlen($number) - 1; | |
$parity = $numDigits % 2; | |
for ($i = $numDigits; $i >= 0; $i--) { | |
$digit = substr($number, $i, 1); | |
if (!$parity == ($i % 2)) {$digit <<= 1;} | |
$digit = ($digit > 9) ? ($digit - 9) : $digit; | |
$sum += $digit; | |
} | |
return ($sum % 10); | |
} | |
public static function check($number) { | |
return (0 == self::calc($number)); | |
} | |
} | |
-----BEGIN PGP SIGNATURE----- | |
Version: GnuPG v2 | |
iQIcBAEBCAAGBQJXQeVIAAoJEC4SA5pJ49cLVUsP/39z2ICuVK85VvjDjtrx1bU0 | |
MaIMDjE+uw8SGWCi2bMJL0KhiiU0hdnyMWDp1HxF1nMlONm2ebCa8uPFAMTDQCz6 | |
agx09JFCzinOw78N4CdBFdoWPGiNKjl+FeorEdeLRqJes150oL0trYK4zoBFWBwK | |
8VdQx9h/niEUx6qc4F9G3ofaJcxYm9ybPY2X8t8SePt+sUnZ6zSrobUWjDtPkIxx | |
4ncVWJ6IACSGoWNbM+d2Xdy7x4TmtE9VE7aEPgtc/FBACQNY7Zwebe/LtjWZC6qk | |
tcAAz7stMuTzzRtmYA7nVPfZHT77VWqCwyy72omtr2++WWaaOlxksib4wHQWCz9G | |
0Set+Zfi/zvn7hkm0913WBfXvv90d9vx6If6K5MMdJhw28PRO1vK6+MV3g5cMT3H | |
KG86RAfNi6S94VBiv/X5dJLE681Gy2NXqbd+DLPPd1IMbNLBWV1/4V4c3ttQ/s06 | |
ejnABPbru7HR+ahYWkrOxAeKMenMwGggKJe49UxPKPqm7hzRJsxI4279biMa12AB | |
AEJsicuquFuUVhZ5SAJATZeJBTBGy6PJibj81FbCPO20oW2i6XtEz4d9YMpFC95V | |
nOXS0apm21lGb9gfd/hK1E7VCFb393tm/GaxBH+4xG8Re+ExpYjz1GG30yHKN0sj | |
vqplb6Buu0gMNbtG+xcC | |
=qP4X | |
-----END PGP SIGNATURE----- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment