Created
April 27, 2009 07:54
-
-
Save davidreuss/102383 to your computer and use it in GitHub Desktop.
url shortener built on custom base system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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