Posted by McKirahan on 03/01/07 10:42
"Kris" <Kristian.Nissen@gmail.com> wrote in message
news:1172739986.866089.231740@j27g2000cwj.googlegroups.com...
> I need to create a valid mod10 or luhn number that can be attached to
> an invoice, I have found several examples showing how to validate a
> credit card number or validate a number using mod10/luhn.
>
> Example:
> invoice number = 700123
> invoice check number 700123(checknumber)
>
> that check number should make the entire invoice number able to
> validate using mod10/luhn
Google is your friend.
I cobbled this together from several sources; will it help?
Validate it thoroughly before relying on it!
<?php
$inv = "700123";
echo $inv . checkdigit($inv);
function checkdigit($num) {
/*
* 1. Reverse the number
* 2. Multiply all the digits in odd positions (The first digit, the third
digit, etc) by 2.
* 3. If any one is greater than 9 subtract 9 from it.
* 4. Sum those numbers up
* 5. Add the even numbered digits (the second, fourth, etc) to the number
you got in the previous step
* 6. The check digit is the amount you need to add to that number to make
a multiple of 10.
* So if you got 68 in the previous step the check digit would be 2.
* You can calculate the digit in code using checkdigit = ((sum / 10 +
1) * 10 - sum) % 10
* -- http://www.darkcoding.net/index.php/credit-card/luhn-formula/
*/
$sum = 0;
$pos = 0;
$rev = strrev($num);
$len = strlen($num);
if ($len % 2 == 0) $len += 1;
while ($pos < $len) {
$odd = $rev[$pos] * 2;
if ($odd > 9) {
$odd -= 9;
}
$sum += $odd;
if ($pos != ($len - 2)) {
$sum += $rev[$pos +1];
}
$pos += 2;
}
return ((floor($sum/10) + 1) * 10 - $sum) % 10;
}
?>
[Back to original message]
|