短網址流行的已經有一段時間了,以前做新浪微博應用的時候就有接觸,但沒有搞清楚,最近再次接觸到這個東東,仔細研究了下,發現短網址其實也挺容易的。下面就將使用php生成短網址的實現方法做一下記錄。
php生成短域名函數
- public function createRandCode($string) {
- $code = '';
- $hex_code = '1qaz2wsx3edc4rfv5t-gb6yhn7ujm8ik9ol0p_';
- $now = microtime(true) * 10000;
- $strlen = strlen($hex_code);
- $hash_code = hash('sha256', $string);
- // 這里會為編碼定義一個隨機的長度,長度取決于step
- $step = rand(8, 16);
- $count = ceil(strlen($hash_code) / $step);
- for($i = 0; $i < $count; $i++) {
- $start = $i * $step;
- $hex_num = substr($hash_code, $start, $step);
- $num = 0x3fffffff & (1 * '0x' . $hex_num);
- $n = $num % $strlen;
- $code .= $hex_code[$n];
- }
- return $code;
- }
新聞熱點
疑難解答