Search code examples
phpauto-incrementalphanumeric

increment alpha numerically values


Like the title suggests I need to do something like so...

$i++;//we all know this.

$value = 'a';

increment($value);// i need this functionality
//output 
string [ b ]

///here are some more samples, to help you understand...

increment('b'); //output// c
increment('z'); //output// A [capital A not fussy but would be good :) ]
increment('9'); //output// a1
increment('a1'); //output// a2
increment('aa1'); //output// aa2

and so on... UPDATE

well lets say I use numeric values $id++; I would end up with a massive number eventuall 1310743942525; which can take alot more space than say `ab34c9" im trying to save length of characters to save on db ...


Solution

  • You try to treat it as a base 62 number:
    http://www.pgregg.com/projects/php/base_conversion/base_conversion.php

    with source code at

    http://www.pgregg.com/projects/php/base_conversion/base_conversion.inc.phps

    convert it to decimal, increment, and convert it back to base 62

    UPDATE

    From how I read the code, you could have a workflow like this:

    $value = 'ab8Zb';
    $value_base10 = base_base2dec($value, 62);
    $value_base10++;
    $value = base_dec2base($value_base10, 62); // should be 'ab8Zc'