0

how does the "tiny url" sites get so tiny ID url ?

i mean this : blabla.com/JH7

how can i get to such result? a functionality that is like md5 that does not repeat it self. thanks in advance!

1

3 Answers 3

1

For example you can simply iterate trough string:

php > $str = 'aaa';
php > $str++;
php > echo $str;
aab

The another option is to prepare function which will generate random strings containing of a-zA-Z0-9 and than generate few millions of them into db (so you could just use them when needed) or do it in loop:

while( 1){
    $rand = randomString();
    if( isUnique( $rand)){
        break;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you , that's what i was looking for! but i got a question stackoverflow.com/questions/9083311/…
1
  1. Make a database table with the columns short_url and url.
  2. Start by inserting the record a, example.com.
  3. Increment short_url with each new entry (b, c, ..., a1 ...).

That's basically how these services work.

1 Comment

don't forget to use uppercase letters A-Z
1

They use base36 encoding to convert an integer to a compact string like that.

Using PHP:

<?php
$id     = 18367;
$base36 = base_convert($id, 10, 36); // convert to base36 "e67"
$base10 = base_convert($base36, 36, 10); // "e67" back to base 10, $id

As stated by deceze, base62 is also suitable which gives you a character set of a-zA-Z0-9 instead of just a-z0-9 like base36 does.

2 Comments

Probably more like base62 (upper/lower case)... :)
@deceze That would make more sense, I looked around but didn't see any tinyurl's with mixed case. I think they went with 36 so there is no ambiguity between entering upper case vs lower case. The wiki article says they use base36 but I don't know if that is still correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.