Reputation: 53
I would like to build a php script that automatically generates a new id by increasing the previous by 1. eg: A0009 becomes A0010 and A9999 becomes B0000
I have written one that works but it doesn't go over 5 chars long: eg: Z9999 should go to A00000 and so on.
Any suggestions? here is my snippet:
<?php
function replaceChar($string2replace)
{
$charLength = strlen($string2replace)-1;
$charAt = array();
$charAt[4] = substr($string2replace, -1);
$charAt[3] = substr($string2replace, -2,1);
$charAt[2] = substr($string2replace, -3,1);
$charAt[1] = substr($string2replace, -4,1);
$charAt[0] = substr($string2replace, 0,1);
if($charAt[4] < 9)
{
$string2replace = substr_replace($string2replace,$charAt[4]+1,$charLength);
}
else
{
$charAt[4] = 0;
$string2replace = substr_replace($string2replace,$charAt[4],$charLength);
if($charAt[3] < 9)
{
$string2replace = substr_replace($string2replace,$charAt[3]+1,$charLength- 1,1);
}
else
{
$charAt[3] = 0;
$string2replace = substr_replace($string2replace,$charAt[3],$charLength-1,1);
if($charAt[2] < 9)
{
$string2replace = substr_replace($string2replace,$charAt[2]+1,$charLength-2,1);
}
else
{
$charAt[2] = 0;
$string2replace = substr_replace($string2replace,$charAt[2],$charLength-2,1);
if($charAt[1] < 9)
{
$string2replace = substr_replace($string2replace,$charAt[1]+1,$charLength-3,1);
}
else
{
$charAt[1] = 0;
$string2replace = substr_replace($string2replace,$charAt[1],$charLength-3,1);
}
if($charAt[0] < 'z')
{
$charAt[0] ++;
$string2replace = substr_replace($string2replace,$charAt[0],$charLength-4,1);
}
else
{
$charAt[0] = 'a';
$string2replace = substr_replace($string2replace,$charAt[0],$charLength-4,1);
}
}
}
}
return $string2replace;
}
$string2begin = 'A9999';
$generatedString = replaceChar($string2begin);
echo $string2begin . "<br />" . $generatedString;
?>
Upvotes: 0
Views: 183
Reputation: 20873
Your ID numbering scheme seems rather contrived, where the high-order digit is A-Z
and the remaining digits are 0-9
. If I understand that pattern correctly, this seems to do the trick:
function incrementID($id)
{
$letter = $id[0];
$number = substr($id, 1);
$newNum = str_pad($number + 1, strlen($number), '0', STR_PAD_LEFT);
// increase number only
if (strlen($number) == strlen($newNum))
return $letter . $newNum;
// increase ID length ('Z' to 'A')
if ($letter == 'Z')
return 'A' . str_repeat('0', strlen($number) + 1);
// change letter
$newLetter = chr(ord($letter) + 1);
return $newLetter . str_repeat('0', strlen($number));
}
printf("%s\n", incrementID('A0009')); // 'A0010'
printf("%s\n", incrementID('A9999')); // 'B0000'
printf("%s\n", incrementID('Z9999')); // 'A00000'
Even though your examples didn't fit this, I first assumed you really just wanted a base-36 number (any digit could be 0-9,A-Z
, where A
is 10 and Z
is 35). Working with numbers in base-36 is easy because you can use base_convert()
to convert them to customary base-10. This is all you would need to do to increment base-36 numbers:
function incrementBase36($id)
{
$numVal = base_convert($id, 36, 10);
$newId = base_convert($numVal + 1, 10, 36);
return strtoupper($newId);
}
printf("%s\n", incrementBase36('A0009')); // 'A000A'
printf("%s\n", incrementBase36('A9999')); // 'A999A'
printf("%s\n", incrementBase36('Z9999')); // 'Z999A'
printf("%s\n", incrementBase36('AZZZZ')); // 'B0000'
printf("%s\n", incrementBase36('ZZZZZ')); // '100000'
Upvotes: 3