Thread: Short URLs
View Single Post

  #3 (permalink)  
Old 10-03-2005
Kimmo Laine
 
Posts: n/a
Default Re: Short URLs

"Geoff Berrow" <blthecat@ckdog.co.uk> wrote in message
news:kck1k156tch9btrf6r87ej3g276cvrjf51@4ax.com...
>I noticed that Message-ID:
> <1128315722.466228.50050@o13g2000cwo.googlegroups. com> from
> kai.hendry@gmail.com contained the following:
>
>>I just wondered is there some useful maths / spec behind how to come up
>>with a unique (short) ID for a resource? I would like to avoid a DBMS,
>>so a hash or something?
>>
>>Yes I saw http://freshmeat.net/projects/shortlink/ but that's in Perl,
>>I want PHP.

>
> Which also uses a database. You want something that will compress the
> text of the URL in such a way that you can reverse the algorithm and get
> the original string back. Interesting challenge.



Not necessarily, he could mean just a flat file system instead of a dbms.

substr(base_convert(time(),10,36), -5);
will produce (relatively) short, five-character id's. It simply takes the
current timestamp and encodes it to a 36base 0-9 a-z representation. I left
out the first digit since it has relatively no effect. If you wanna be
really effective, you'll have a simple counter and you'll start from zero
and increase each time you add a link.

file_put_contents('counter.txt', $ctr =
(file_get_contents('counter.txt.')+1));
base_convert($ctr,10,36);

That'll keep 'em short.

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
antaatulla.sikanautaa@gmail.com.NOSPAM.invalid


Reply With Quote