Luhn (modulo 10) check digit calculator
------=_NextPart_000_00D8_01CA8E4A.CEB430C0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi all. Perhaps a lazy request, but does anybody have some cut-n-paste =
code to calculate a Luhn check-digit?
It's a check-digit often added to the end of things like credit card or =
account numbers to make detecting typo's a bit easier.
I found lots of code to validate a number once the check-digit is =
applied, but nothing to calculate the digit in the first place.
Thanks in advance !
Angus
------=_NextPart_000_00D8_01CA8E4A.CEB430C0--
Re: Luhn (modulo 10) check digit calculator
Angus Mann wrote:
> Hi all. Perhaps a lazy request, but does anybody have some cut-n-past=
e
> code to calculate a Luhn check-digit?
>
http://de.wikipedia.org/wiki/Luhn-Algorithmus
(several examples, including one in PHP).
/Per
--
Per Jessen, Z=C3=BCrich (0.0=C2=B0C)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Luhn (modulo 10) check digit calculator
2010/1/5 Angus Mann <angusmann [at] pobox.com>:
> Hi all. Perhaps a lazy request, but does anybody have some cut-n-paste code to calculate a Luhn check-digit?
>
> It's a check-digit often added to the end of things like credit card or account numbers to make detecting typo's a bit easier.
>
> I found lots of code to validate a number once the check-digit is applied, but nothing to calculate the digit in the first place.
>
> Thanks in advance !
> Angus
>
>
Modified version found at
http://blog.planzero.org/2009/08/luhn-modulus-implementation -php/
<?php
/* Luhn algorithm number checker - (c) 2005-2008 - planzero.org *
* This code has been released into the public domain, however please *
* give credit to the original author where possible. */
function luhn_check($number) {
// If the total mod 10 equals 0, the number is valid
return (luhn_process($number) % 10 == 0) ? TRUE : FALSE;
}
function luhn_process($number) {
// Strip any non-digits (useful for credit card numbers with spaces
and hyphens)
$number=preg_replace('/\D/', '', $number);
// Set the string length and parity
$number_length=strlen($number);
$parity=$number_length % 2;
// Loop through each digit and do the maths
$total=0;
for ($i=0; $i<$number_length; $i++) {
$digit=$number[$i];
// Multiply alternate digits by two
if ($i % 2 == $parity) {
$digit*=2;
// If the sum is two digits, add them together (in effect)
if ($digit > 9) {
$digit-=9;
}
}
// Total up the digits
$total+=$digit;
}
return $total;
}
function luhn_checkdigit($number) {
return 10 - (luhn_process($number . '0') % 20);
}
echo '49927398716 is ', (luhn_check('49927398716') ? 'Valid' :
'Invalid'), PHP_EOL;
echo 'The check digit for 4992739871 is ',
luhn_checkdigit('4992739871'), PHP_EOL;
?>
outputs ...
49927398716 is Valid
The check digit for 4992739871 is 6
--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Luhn (modulo 10) check digit calculator
2010/1/5 Richard Quadling <rquadling [at] googlemail.com>:
> 2010/1/5 Angus Mann <angusmann [at] pobox.com>:
>> Hi all. Perhaps a lazy request, but does anybody have some cut-n-paste c=
ode to calculate a Luhn check-digit?
>>
>> It's a check-digit often added to the end of things like credit card or =
account numbers to make detecting typo's a bit easier.
>>
>> I found lots of code to validate a number once the check-digit is applie=
d, but nothing to calculate the digit in the first place.
>>
>> Thanks in advance !
>> Angus
>>
>>
>
> Modified version found at
> http://blog.planzero.org/2009/08/luhn-modulus-implementation -php/
>
> <?php
> /* Luhn algorithm number checker - (c) 2005-2008 - planzero.org =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0*
> =C2=A0* This code has been released into the public domain, however pleas=
e =C2=A0 =C2=A0 =C2=A0*
> =C2=A0* give credit to the original author where possible. =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0*/
>
> function luhn_check($number) {
> =C2=A0// If the total mod 10 equals 0, the number is valid
> =C2=A0return (luhn_process($number) % 10 =3D=3D 0) ? TRUE : FALSE;
>
> }
>
> function luhn_process($number) {
>
> =C2=A0// Strip any non-digits (useful for credit card numbers with spaces
> and hyphens)
> =C2=A0$number=3Dpreg_replace('/\D/', '', $number);
>
> =C2=A0// Set the string length and parity
> =C2=A0$number_length=3Dstrlen($number);
> =C2=A0$parity=3D$number_length % 2;
>
> =C2=A0// Loop through each digit and do the maths
> =C2=A0$total=3D0;
> =C2=A0for ($i=3D0; $i<$number_length; $i++) {
> =C2=A0 =C2=A0$digit=3D$number[$i];
> =C2=A0 =C2=A0// Multiply alternate digits by two
> =C2=A0 =C2=A0if ($i % 2 =3D=3D $parity) {
> =C2=A0 =C2=A0 =C2=A0$digit*=3D2;
> =C2=A0 =C2=A0 =C2=A0// If the sum is two digits, add them together (in ef=
fect)
> =C2=A0 =C2=A0 =C2=A0if ($digit > 9) {
> =C2=A0 =C2=A0 =C2=A0 =C2=A0$digit-=3D9;
> =C2=A0 =C2=A0 =C2=A0}
> =C2=A0 =C2=A0}
> =C2=A0 =C2=A0// Total up the digits
> =C2=A0 =C2=A0$total+=3D$digit;
> =C2=A0}
>
> =C2=A0return $total;
> }
>
> function luhn_checkdigit($number) {
> =C2=A0return 10 - (luhn_process($number . '0') % 20);
> }
>
>
> echo '49927398716 is ', (luhn_check('49927398716') ? 'Valid' :
> 'Invalid'), PHP_EOL;
> echo 'The check digit for 4992739871 is ',
> luhn_checkdigit('4992739871'), PHP_EOL;
> ?>
>
>
> outputs ...
>
> 49927398716 is Valid
> The check digit for 4992739871 is 6
>
>
>
> --
> -----
> Richard Quadling
> "Standing on the shoulders of some very clever giants!"
> EE : http://www.experts-exchange.com/M_248814.html
> Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D2134=
74731
> ZOPA : http://uk.zopa.com/member/RQuadling
>
And that 20 is a typo. Sorry.
function luhn_checkdigit($number) {
return 10 - (luhn_process($number . '0') % 10);
}
--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php