Alpha numeric increment

Like the name, I need to do something like this ...

$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 let's say I use the numerical values ​​of $ ID ++; As a result, I get a huge number of events 1310743942525 ; which may take up more space than saying "ab34c9" im trying to keep the length of the characters to be stored on db ...

+4
source share
3 answers

You are trying to treat it as base number 62:
http://www.pgregg.com/projects/php/base_conversion/base_conversion.php

with source code

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

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

UPDATE

From the way 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' 
+3
source

If all you are trying to do is save the database space, think about it.

In MySQL, you may have a field of type UNSIGNED BIGINT . The maximum size of this field is 18446744073709551615, and the storage space is only 8 bytes.

If you were to convert this number (1,484 x 10 ^ 19) to base-62, it would be represented as LygHa16AHYF . To store the converted number, you will need CHAR(11) (11 bytes) or VARCHAR(11) (12 bytes).

If you used VARCHAR for a field type, smaller numbers would take up less space, but for larger numbers it really takes more. 8 bytes for a huge number are pretty minimal anyway. I would save all my efforts and just make the database field UNSIGNED BIGINT .

+2
source

You can use the ascii codes of each letter. This is just a simple example that will show you the idea, of course, for this you need a lot of modifications if you want to increase "aa1" by "aa2", but it’s impossible, but it’s not: P You just need to write a few conditions.

 function increment($value) { if(strlen($value)>1) return false; $asciiCode = ord($value); return chr($asciiCode + 1); } 

http://www.asciitable.com/ - ASCII code table :)

0
source

All Articles