php-extended / php-luhn
A library to work with luhn's algorithm for validating numbers
Requires
- php: >=7.0
This package is auto-updated.
Last update: 2021-07-12 20:42:57 UTC
README
A library to work with luhn's algorithm for validating numbers
Installation
The installation of this library is made via composer.
Download composer.phar
from their website.
Then add to your composer.json :
"require": {
...
"php-extended/php-luhn": "^2",
...
}
Then run php composer.phar update
to install this library.
The autoloading of all classes of this library is made through composer's autoloader.
Basic Usage
You may use this library this following way:
use PhpExtended\Luhn\Luhn;
use PhpExtended\Luhn\LuhnException;
$number = '<put here your numeric string>';
$luhn = new Luhn();
try
{
$valid = $luhn->validate($number);
}
catch(LuhnException $e)
{
// does something
}
The $valid
value will hold a boolean true if the number is valid from the luhn formula, false if it isnt. This method
throws an exception if the given argument to the validate()
method is not a numeric string or an integer.
You may also use the Luhn
class to sign a number (i.e. adding the check digit at the end of it) with the sign()
function.
License
MIT (See license file).