Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding like base36 including uppercase

Tags:

php

I am using base36 to shorten URLs. I have an id of a blog entry and convert that id to base36 to make it smaller. Base36 only includes lowercase letters. How can I include uppercase letters? If I use base64_encode it actually makes the string longer.

like image 374
mistero Avatar asked Sep 26 '09 13:09

mistero


1 Answers

you can find examples of source-code to create short-urls containing letters (both lower and upper case) and number on those two articles, for instance :

  • Create short IDs with PHP - Like Youtube or TinyURL
  • Building a URL Shortener

Here is the portion of code used in that second article (quoting) :

$codeset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$base = strlen($codeset);
$n = 300;
$converted = "";

while ($n > 0) {
  $converted = substr($codeset, ($n % $base), 1) . $converted;
  $n = floor($n/$base);
}

echo $converted; // 4Q

And you can pretty easily encapsulate this in a function -- only thing to consider is that $n is to be received as a parameter :

function shorten($n) {
    $codeset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $base = strlen($codeset);
    $converted = "";
    while ($n > 0) {
      $converted = substr($codeset, ($n % $base), 1) . $converted;
      $n = floor($n/$base);
    }
    return $converted;
}

And calling it this way :

$id = 123456;
$url = shorten($id);
var_dump($url);

You get :

string 'w7e' (length=3)

(You can also add some other characters, if needed -- depending on what you want to get in your URLs)


Edit after the comment :

Reading through the second article (from which I got the shortening code), you'll find the code that does the un-shortening.

Encapsulating that code in a function shouldn't be that hard, and might get you something like this :

function unshorten($converted) {
    $codeset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $base = strlen($codeset);
    $c = 0;
    for ($i = strlen($converted); $i; $i--) {
      $c += strpos($codeset, substr($converted, (-1 * ( $i - strlen($converted) )),1)) 
            * pow($base,$i-1);
    }
    return $c;
}

And calling it with a shortened-url :

$back_to_id = unshorten('w7e');
var_dump($back_to_id);

Will get you :

int 123456
like image 146
Pascal MARTIN Avatar answered Sep 19 '22 16:09

Pascal MARTIN