Encoding like base36 including uppercase

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

function dec2any( $num, $base=62, $index=false ) {

    // Parameters:
    //   $num - your decimal integer
    //   $base - base to which you wish to convert $num (leave it 0 if you are providing $index or omit if you're using default (62))
    //   $index - if you wish to use the default list of digits (0-1a-zA-Z), omit this option, otherwise provide a string (ex.: "zyxwvu")

    if (! $base ) {
        $base = strlen( $index );
    } else if (! $index ) {
        $index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,0 ,$base );
    }
    $out = "";
    for ( $t = floor( log10( $num ) / log10( $base ) ); $t >= 0; $t-- ) {
        $a = floor( $num / pow( $base, $t ) );
        $out = $out . substr( $index, $a, 1 );
        $num = $num - ( $a * pow( $base, $t ) );
    }
    return $out;
}

Shamelessly borrowed from a commenter on PHP's base_convert() page (base_convert() only works up to base 32).

Tags:

Php