Skip to content

Instantly share code, notes, and snippets.

@dervn
Created October 29, 2010 05:33
Show Gist options
  • Save dervn/652979 to your computer and use it in GitHub Desktop.
Save dervn/652979 to your computer and use it in GitHub Desktop.
tinyurl
public static function shorturl($input)
{
$base32 = array ( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5' );
$hex = md5($input);
$hexLen = strlen($hex);
$subHexLen = $hexLen / 8;
$output = array();
for ($i = 0; $i < $subHexLen; $i++)
{
$subHex = substr ($hex, $i * 8, 8);
$int = 0x3FFFFFFF & (1 * ('0x'.$subHex));
$out = '';
for ($j = 0;
$j < 5; $j++)
{
$val = 0x0000001F & $int;
$out .= $base32[$val];
$int = $int >> 5;
}
$output[] = $out;
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment