Last active
December 12, 2015 09:39
-
-
Save mamor/4753064 to your computer and use it in GitHub Desktop.
短縮URLを生成するメソッド
参考: http://workline.xii.jp/texts/oneday_url/
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
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