PHP - Generate an 8 character hash from an integer

doc picture doc · Mar 26, 2010 · Viewed 28.7k times · Source

Is there a way to take any number, from say, 1 to 40000 and generate an 8 character hash?

I was thinking of using base_convert but couldn't figure out a way to force it to be an 8 character hash.

Any help would be appreciated!

Answer

Nathan Osman picture Nathan Osman · Mar 26, 2010

Why don't you just run md5 and take the first 8 characters?

Because you are wanting a hash, it doesn't matter whether portions are discarded, but rather that the same input will produce the same hash.

$hash = substr(md5($num), 0, 8);