php3-list | 199807

Re: [PHP3] Credit Card From: Stefan Powell (spowell <email protected>)
Date: 07/30/98

Stefan Powell wrote:
> <?php
> //Stefan's slightly obfuscated, but much improved LUHNMod10
> function LUHNMod10($ccnum) {
> if (!isset($ccnum)) return FALSE;
> $sum = 0;
> $len = strlen($ccnum);
> $ccnum=ereg_replace('[^[:digit:]]+','',$ccnum);
> for($idx=0;$idx<$len;$idx++) {
>...

Whoa! HUGE error in that function... I should set the length after
stipping the non-digits.

//Stefan's slightly obfuscated, but much MUCH improved LUHNMod10
function LUHNMod10($ccnum) {
  if (!isset($ccnum)) return FALSE;
  $ccnum=ereg_replace('[^[:digit:]]+','',$ccnum);
  $sum = 0;
  $len = strlen($ccnum);
  for($idx=0;$idx<$len;$idx++) {
    $digit = substr($ccnum,$idx,1);
    if ($idx%2) {$sum += ($digit >= 5) ? 1+(($digit*2)%10) : $digit*2;}
    else {$sum += $digit;}
  }
  if ($sum%10) return FALSE;
  return TRUE;
}

--Stef

--
PHP 3 Mailing List   http://www.php.net/
To unsubscribe send an empty message to php3-unsubscribe <email protected>
To subscribe to the digest list:  php3-digest-subscribe <email protected>
For help: php3-help <email protected>  Archive:  http://www.php.net/mailsearch.php3