Skip to content

Instantly share code, notes, and snippets.

@astroanu
Last active August 29, 2015 14:13
Show Gist options
  • Save astroanu/15caf94ad60da316df58 to your computer and use it in GitHub Desktop.
Save astroanu/15caf94ad60da316df58 to your computer and use it in GitHub Desktop.
generate random id
function getShortenForm($url){
$ignore = array('I','0','O','1');
$alphabet = array_merge(range('A','Z'),array_map(function($i){return (string)$i;},range(0,9)));
$parsing_url = md5($url.'-'.rand().'-'.rand().'-'.microtime(true));
$parsing_url = substr($parsing_url, strlen($parsing_url)/2-1,strlen($parsing_url)).substr($parsing_url, 0,strlen($parsing_url)/2);
$shorten = strtoupper(substr(base_convert($parsing_url,16, 36),0,self::MAX_CHARS));
$cleaned = array_diff($alphabet, $ignore);
$cleaned = array_values($cleaned);
$replaceble = array();
array_walk($ignore,function($val,$key) use (&$replaceble,$cleaned){
array_push($replaceble,$cleaned[rand(0, count($cleaned)-1)]);
});
$returnShorten = str_replace($ignore, $replaceble, $shorten);
return $returnShorten;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment