Base36 Encoding for Tiny URLs With PHP

| 2 TrackBacks
While adding permalink and Twitter support to Onyx last week, I realized that PHP can easily convert a number between two arbitrary bases using its built in base_convert function.  This was crucial for me because it meant that I could quickly generate a base-36 encoded string from a standard base-10 number:

$tiny = base_convert($key, 10, 36);

As Wikipedia explains, "...the choice of 36 is convenient in that the digits can be represented using the Arabic numerals 0-9 and the Latin letters A-Z.  Base 36 is therefore the most compact case-insensitive alphanumeric numeral system using ASCII characters...".  Not to mention, it's the highest radix most languages support by default.

I built Onyx to maintain a list of files (as inode's) in a MySQL database table.  Luckily, the inode's are stored in this table as auto incrementing BIGINT(20)'s, which are of course, base-10 encoded numbers:

CREATE TABLE files (

f_inode BIGINT(20) NOT NULL AUTO_INCREMENT,
...

INDEX inode_index ( f_inode ),
PRIMARY KEY ( f_inode ),
...

) TYPE=InnoDB;

Base-36 encoding my inode's is key because this means that I can easily, for example, convert base-10 inode 3,032 into "2c8" base-36 for a tiny URL.  From there, it's simply a matter of using the base-36 encoded string in a tiny/shortened permalink URL to refer to the correct file/inode on Onyx.  Yes, this is a poor example because I'm only saving 1 character in a supposedly shorter URL ("3032" vs "2c8").  However, this makes a lot more sense with larger base-10 numbers like 105,621,983 which condenses nicely into "1qvufz" base-36.  Personally, I'd rather see /1qvufz on the end of a "tiny" URL than /105621983.

Enjoy.

Did You Find this Helpful?

Did you find this post helpful, or at least, interesting?

  

Send Mark a Direct Message

If you'd like to send me a direct message, please do so below. However, I do not publicly post comments or messages submitted directly to me. So, if you're going to try to SPAM me, or my blog, you're pretty much wasting your time.

400 characters remaining

Error

About Mark

A Silicon Valley native, Mark Kolich is a full-time Software Engineer, a casual entrepreneur, and a consultant for hire. A web technologies expert, his current focus is on building powerful and robust cloud-driven web-applications using Java, PHP, Perl, AJAX, DHTML, CSS, and JavaScript. His favorite programming languages are PHP, Java and JavaScript. He uses Linux, enjoys biking to work, loves building great software, and always writes elegant, readable, and maintainable code.

2 TrackBacks

Onyx (beta) from Mark S. Kolich on November 6, 2009 12:15 PM

If you follow me on Twitter, you may have noticed I silently launched Onyx a few weeks ago.  In a nutshell, as described on the Onyx homepage ..."Onyx is a social file management tool I built to help me keep... Read More

A blog reader recently contacted me via email and asked, "hey, how does your koli.ch tiny URL thing work?"  Well, I would be happy to explain.  As previously discussed here, and here, I'm not using Apache's mod_rewrite RewriteMap engine. ... Read More

Twitter (@markkolich)

Translate

About this Entry

This page contains a single entry by Mark Kolich published on October 28, 2009 5:15 PM.

Bundle Java (the JRE) and Launch a Java App with 7zip SFX! ... (Convert Java Apps to an Executable, sort of) was the previous entry in this blog.

Controlling When and Where Java Writes Out its Permgen and Heap to Disk is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.