How to create URL-able unique id that is as short as possible?

Viewed 2252

For my domain, I'm trying to develop an ID generator that creates a unique ID, which can then be used for a shortlink (just as a URL shortener does).

I thought about using md5() or uniqueid() to create that ID. But to keep URLs as short as possible, this wouldn't be smart, as it passes out a huge amount of IDs, so the string/ID would get longer without any necessity.

Now PHP's string incrementation offers an interesting way. I could do:

$d = 'A08';
for ($n=0; $n<60; $n++) {
    echo ++$d . PHP_EOL;
}
// Outputs: A09 .. A68

But this only gives me all strings from A09 to A68. So I set $d = '000' in order to start from there. But now it only returns integers from 1 to60, instead of strings.

How can I force PHP to increment from 000 via AAA to ZZZ?

So to speak, I want an id than runs from 0 via 9 via A to Z and a to z.

0, 1, 2, .., 9, A, .., Z, a, .., z

Then the second character shall be added to the id:

00, 01, 02, .., 09, 0A, .., 0Z, 0a, .., 0z
3 Answers
Related