Here's the Luhn in PHP. Check it out.
<?php
function luhnCheckSum($accountNumber)
{
$accountArray = array();
$total = 0;
$i = 0;
for($i = 0; $i < strlen($accountNumber) - 1; $i++)
{
if (!isset($accountArray[$i])) {$accountArray[$i] = 0;}
$accountArray[$i] = intval(substr($accountNumber, $i,1));
if ( ($accountArray[$i] > 9) && ($i%2 == 1) )
{
$accountArray[$i] = 1 + $accountArray[$i] % 10;
}
$total = $total + $accountArray[$i];
}
$total *= 9;
$lastDigit = 0;
$lastDigit = $total % 10;
$checkSumDigit = 0;
$checkSumDigit = intval(substr($accountNumber, 9, 1));
if ($lastDigit === $checkSumDigit)
{
return true;
}
else
{
return false;
}
}
?>
<?php
function luhnCheckSum($accountNumber)
{
$accountArray = array();
$total = 0;
$i = 0;
for($i = 0; $i < strlen($accountNumber) - 1; $i++)
{
if (!isset($accountArray[$i])) {$accountArray[$i] = 0;}
$accountArray[$i] = intval(substr($accountNumber, $i,1));
if ( ($accountArray[$i] > 9) && ($i%2 == 1) )
{
$accountArray[$i] = 1 + $accountArray[$i] % 10;
}
$total = $total + $accountArray[$i];
}
$total *= 9;
$lastDigit = 0;
$lastDigit = $total % 10;
$checkSumDigit = 0;
$checkSumDigit = intval(substr($accountNumber, 9, 1));
if ($lastDigit === $checkSumDigit)
{
return true;
}
else
{
return false;
}
}
?>
No comments:
Post a Comment