/Luhn-Algorithm

Implementation of the Luhn Algorithm in PHP

Primary LanguagePHPMIT LicenseMIT

Luhn Algorithm in PHP

This is an implementation of the Luhn Algorithm in PHP. The Luhn Algorithm is used to validate things like credit cards and national identifcation numbers. More information on the algorithm can be found at Wikipedia

Installation

Can be installed using composer:

"nekman/luhn-algorithm": "2.*"

Usage

Use the class like this:

$luhn = new \Nekman\LuhnAlgorithm\LuhnAlgorithm('123456789');
$luhn->isCompletelyValid();

The class contains some static functions as well. This will return the Luhn checksum of a number:

$number = '123456789';
$luhnCheckSum = \Nekman\LuhnAlgorithm\LuhnAlgorithm::calculateChecksum($number);

Personnummer

If you'd like to validate the input to the class, extend it and do a regex check. In the file Personnummer.php, I have extended the class to make sure that the input is a valid Swedish national security id.