Created
August 10, 2014 04:53
-
-
Save allex/1ec4083d5c3321ceb96a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
// Author: Allex Wang (http://iallex.com) | |
// GistID: 1ec4083d5c3321ceb96a | |
// GistURL: https://gist.github.com/1ec4083d5c3321ceb96a | |
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 < 6; $j++) { | |
$val = 0x0000001F & $int; | |
$out .= $base32[$val]; | |
$int = $int >> 5; | |
} | |
$output[] = $out; | |
} | |
return $output; | |
} | |
var_dump(shorturl("http://www.codesphp.com/php-category/algorithms/php-short-url-algorithm-implementation.html?utm_medium=twitter&utm_source=twitterfeed")); | |
var_dump(shorturl("https://raw.github.co")); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment