Skip to content

Instantly share code, notes, and snippets.

@davidreuss
Created April 27, 2009 07:54
Show Gist options
  • Save davidreuss/102383 to your computer and use it in GitHub Desktop.
Save davidreuss/102383 to your computer and use it in GitHub Desktop.
url shortener built on custom base system
// Thanks: http://snook.ca/archives/php/url-shortener
$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
$c = 0;
for ($i = strlen($converted); $i; $i--) {
$c += strpos($codeset, substr($converted, (-1 * ( $i - strlen($converted) )),1))
* pow($base,$i-1);
}
echo $c; // 300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment