Luhn (modulo 10) check digit calculator

Luhn (modulo 10) check digit calculator

am 05.01.2010 12:05:28 von Angus Mann

------=_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

am 05.01.2010 12:11:51 von Per Jessen

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?
>=20

http://de.wikipedia.org/wiki/Luhn-Algorithmus

(several examples, including one in PHP).


/Per

--=20
Per Jessen, Zürich (0.0°C)


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Luhn (modulo 10) check digit calculator

am 05.01.2010 12:18:26 von Richard Quadling

2010/1/5 Angus Mann :
> 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/

/* 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

am 05.01.2010 12:19:20 von Richard Quadling

2010/1/5 Richard Quadling :
> 2010/1/5 Angus Mann :
>> 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/
>
> > /* Luhn algorithm number checker - (c) 2005-2008 - planzero.org   =
         *
>  * This code has been released into the public domain, however pleas=
e      *
>  * 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=3Dpreg_replace('/\D/', '', $number);
>
>  // Set the string length and parity
>  $number_length=3Dstrlen($number);
>  $parity=3D$number_length % 2;
>
>  // Loop through each digit and do the maths
>  $total=3D0;
>  for ($i=3D0; $i<$number_length; $i++) {
>    $digit=3D$number[$i];
>    // Multiply alternate digits by two
>    if ($i % 2 == $parity) {
>      $digit*=3D2;
>      // If the sum is two digits, add them together (in ef=
fect)
>      if ($digit > 9) {
>        $digit-=3D9;
>      }
>    }
>    // Total up the digits
>    $total+=3D$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=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);
}



--=20
-----
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