alphanumeric increment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
elliebee
Forum Newbie
Posts: 4
Joined: Mon Mar 20, 2006 2:21 am

alphanumeric increment

Post by elliebee »

Hi guys. I'm hoping you could help me out. I need to generate some ID that is alphanumeric.. Something like

001, 002, 003, ..., 009, 0A0, 0A1, etc....

And I have no idea how to do it. :cry:

Please help!
deadoralive
Forum Commoner
Posts: 28
Joined: Tue Nov 06, 2007 1:24 pm

Post by deadoralive »

You after something like this?

Code: Select all

for( $i = 1; $i <= 50; $i++ )
{
	print str_pad( dechex( $i ), 3, "0", STR_PAD_LEFT) . "<br />";
}
Output:

001, 002, 003, 004, 005, 006, 007, 008, 009, 00a, 00b, 00c, 00d, 00e, 00f, 010, 011, 012, 013, 014, 015, 016, 017, 018, 019, 01a, 01b,
01c, 01d, 01e, 01f......
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: alphanumeric increment

Post by califdon »

elliebee wrote:Hi guys. I'm hoping you could help me out. I need to generate some ID that is alphanumeric.. Something like

001, 002, 003, ..., 009, 0A0, 0A1, etc....

And I have no idea how to do it. :cry:

Please help!
It might help if we knew what you are trying to do. Is it going to be used as a record identifier in a database table?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Also, what is the actual logic you are trying to apply. That would be helpful.
elliebee
Forum Newbie
Posts: 4
Joined: Mon Mar 20, 2006 2:21 am

Post by elliebee »

deadoralive wrote:You after something like this?

Code: Select all

for( $i = 1; $i <= 50; $i++ )
{
	print str_pad( dechex( $i ), 3, "0", STR_PAD_LEFT) . "<br />";
}
Output:

001, 002, 003, 004, 005, 006, 007, 008, 009, 00a, 00b, 00c, 00d, 00e, 00f, 010, 011, 012, 013, 014, 015, 016, 017, 018, 019, 01a, 01b,
01c, 01d, 01e, 01f......


This is actually perfect. Thanks so much!
elliebee
Forum Newbie
Posts: 4
Joined: Mon Mar 20, 2006 2:21 am

Post by elliebee »

actually... what if i wanted it till 'Z'?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

You could use base_convert() to convert your number to decimal (0-9), increment it, then convert back to base 36 (0-z), padding as necessary.
Post Reply