Skip to content

Instantly share code, notes, and snippets.

@mamor
Last active December 12, 2015 09:39
Show Gist options
  • Save mamor/4753064 to your computer and use it in GitHub Desktop.
Save mamor/4753064 to your computer and use it in GitHub Desktop.
短縮URLを生成するメソッド 参考: http://workline.xii.jp/texts/oneday_url/
public static function mk_hash($index, $min_length = 0) {
$ascii_ranges = array(
array(48,57), //0 to 9
array(65,90), //A to Z
array(97,122), //a to z
);
$asciis = array();
foreach ($ascii_ranges as $ascii_range)
{
$asciis = array_merge($asciis, range($ascii_range[0], $ascii_range[1]));
}
$shuffles = array_keys($asciis);
srand(0);
shuffle($shuffles);
$asciis_count = count($asciis);
$i = 0;
$hashs = array();
do
{
$hashs[$i] = chr($asciis[$shuffles[(int) (floor($index / pow($asciis_count, $i)) + $i) % $asciis_count]]);
$i = count($hashs);
}
while (($min_length > $i) || (pow($asciis_count, $i) <= $index));
return implode('', $hashs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment