Happy new year from Palo Alto! Please, I have been working for an inordinately long time trying to create some code for something that should be pretty basic. I have written some nice, simple PHP code that generates a random 11 digit alphanumeric codes, called $elevendigits, like this one:

FFM00976YH5

What I now need to do is write some code that generates a check digit according to the Modulus 10 - - sometimes called Luhn - - standard. Let's call it it $checkdigit. It will be the twelfth digit.

I would have thought this an off-the-shelf function but, can't seem to get anything to work. It would need to:

Convert letters to numbers according to their ordinal place, starting with A=10, such that M=22, for example;

Starting with the right-most digit, multiply every other digit by 2;

Convert any two-digit products from the preceding step into 2 one-digit numbers;

Add all the digits together; and then

Subtract the sum from the next highest number ending in zero. This is the check digit.

Please, can anyone help me with the code necessary to accomplish this? I would have thought it easy, but am failing. Thank you sincerely!

TheRedDevil
—
2013-01-02T01:14:43Z —
#2

It is a pretty simple method to write in PHP, as long as you understand the principle for how the Luhn Algorithm work, which it seems you do.

The difference with your project is that you also need to consider letters, and convert them as you hit any.

I am also not sure why you don't reuse the method you created to make these numbers for the validation as well? You would just need to change it from setting the last digit, to validating it instead.

Give it another try and I'm sure your able to figure it out, remember modulus is your friend. If you run into problems, post your code here and we can take a look and help you out.

So now, we wind up with $randomdigits as only numberals, which is great. But alas, that's all I've been able to accomplish so far. I still need to multiply every other digit of $randomdigits by 2 starting with the right-most digit; convert any two-digit products from the preceding step into 2 one-digit numbers; add all the digits together; and finallysubtract the sum from the next highest number ending in zero to yield the check digit.

I am diligent, but stuck. Any help would be greatly appreciated! Thank you very much.

TheRedDevil
—
2013-01-03T01:20:43Z —
#5

Hmm, ok. Yea, I can see why you might be a little stuck.

You are looking on this from perhaps the wrong perspective, and perhaps a little too procedural If you have the time, I would recommend you to read up on OOP, it would greatly improve your development speed.

I have wrote a function for you that should work for both generating the validation number, as well as validating a complete number as well.

To use the different functions, you would change to the correct return method. One will give you the correct validation number that is missing, the other will tell you if the number is valid (bool).When fetching the correct validation number it is important that you append a "x" (or any other number or character) to the end of the string. I.e. in your first post you mentioned "FFM00976YH5", when passing this number along you would pass it as "FFM00976YH5x", and then just replacing the x afterwards with the correct validation number.

EDIT:Forgot to mention that I have not tested the code, but it should work. In the event you have any problems, please let us know what is happening and we can help sort it out.

StarLion
—
2013-01-03T15:57:43Z —
#6

So, procedurally (Trying to wrap my head around the algorithm in 30 seconds)For a given string

$string = "FFM00976YH5";

1: str_replace letters with numbers . This gives you a pure-digit string of variable length.

$string = str_replace(range('B','Z'),range('11','35'),$string)

2: Iterate to every other digit, starting from the right and walking back to the start.

for ($x = count($string)-1; $x >= 0; $x -= 2)

3: Double the value and Sum (via type juggling)

$sum += array_sum(str_split(($string[$x] * 2)));

4: Apply the math X*9 Mod 10;

$cd = ($sum * 9) % 10;

$cd is now my Check Digit.

Did i miss anything?

TheRedDevil
—
2013-01-03T16:43:34Z —
#7

StarLion said:

Did i miss anything?

Point one would be wrong if you are looking for a true implementation of the algorithm, since you would treat B as 11 and not as 1 and 1 later on.

In your example the string "FFM00976YH5" would be treated as "1515220097634175" so the string goes from being 11 + Validation number to 16 + Validation number, and the length would vary depending on the number of characters in the string/number.

Point two is also not correct as you just illiterate over ever second number, while you also need to add every odd number to the sum as well (without doubling it).

Point three is correct, only thing is that you need the addition for the odd numbers which should not be doubled.

Point four, I am not sure why you are multiplying with 9 here?

On a side note if the validation is only for internal use, any way its implemented will work. The problem would be if you want it to work off other already established systems.

StarLion
—
2013-01-03T16:46:12Z —
#8

Or did i misinterpret your desire to convert the numbers (whole numbers instead of individual digits)? That would change the code to something along the lines of...

For a given string

$string = "FFM00976YH5";

1: Iterate to every other character, starting from the right and walking back to the start.

for ($x = count($string)-1; $x >= 0; $x -= 2) {

2: str_replace letters with numbers .

$char = str_replace(range('B','Z'),range('11','35'),$string[$x])

3: Double the value and Sum (via type juggling)

$sum += array_sum(str_split(($char * 2)));
}

4: Apply the math X*9 Mod 10;

$cd = ($sum * 9) % 10;

$cd is now my Check Digit.

(step 2 and 3 could be combined, but for clarity i left them seperate)

StarLion
—
2013-01-03T16:55:07Z —
#9

TheRedDevil said:

Point one would be wrong if you are looking for a true implementation of the algorithm, since you would treat B as 11 and not as 1 and 1 later on.

Altered in my second post (made while you were posting!)

In your example the string "FFM00976YH5" would be treated as "1515220097634175" so the string goes from being 11 + Validation number to 16 + Validation number, and the length would vary depending on the number of characters in the string/number. hence "variable length".

Point two is also not correct as you just illiterate over ever second number, while you also need to add every odd number to the sum as well (without doubling it).

Ah... yes, that will necessitate a little extra code... below.

Point four, I am not sure why you are multiplying with 9 here?

According to wikipedia, there are two methods of obtaining the value;subtracting the modulo value from 10, or multiplying by 9 and taking the modulo of the result. Either way it's 2 mathematical operations.

On a side note if the validation is only for internal use, any way its implemented will work. The problem would be if you want it to work off other already established systems.

I'm happy to report that I have a working cluster of code, thanks to the generous contributions of StarLion and The Red Devil. Part of the trouble I was having came from not recognizing that my host was running only PHP 4, which caused the str_split call to return undefined. But with encouragement from StarLion and The Red Devil, I plugged through and created a workaround using preg_split. Everything now works fine.

I am extremely grateful to StarLion and The Red Devil, without whom I would not have figured this out. Thank you!

SpacePhoenix
—
2013-01-06T22:30:54Z —
#11

kjm7267 said:

I'm happy to report that I have a working cluster of code, thanks to the generous contributions of StarLion and The Red Devil. Part of the trouble I was having came from not recognizing that my host was running only PHP 4, which caused the str_split call to return undefined. But with encouragement from StarLion and The Red Devil, I plugged through and created a workaround using preg_split. Everything now works fine.

I am extremely grateful to StarLion and The Red Devil, without whom I would not have figured this out. Thank you!

Ask you host if they have the option of running php5 as php4 is no longer supported. If they don't give some thought about moving hosts to one that does.