string compaction in PHP

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
shahryar
Forum Newbie
Posts: 1
Joined: Mon Jun 20, 2005 10:01 am

string compaction in PHP

Post by shahryar »

Hi,

I have around 18 - 20 characters long IDs and I need to compact them, so that I could physically put them on the equipments and for that I need to know about any built-in or any other algorithm/function to use.

The algorithm should generate unique strings for unique IDs and must compact the original string to 5-10 characters.

any help would be truly appreciated.
Thanks in advance.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I would use a combination of substr(), md5(), and rand().

that will *almost* guarantee that it will be unique. You could always check against other ids and if it matches one, you could use rand() again to generate another...if you have 10 digits to play with, the likelyhood that you're going to match another is not very high.

ex:

Code: Select all

$newid = substr(md5($oldid),rand(5,10),8);
Post Reply