思路:
1)將長網址md5產生32位元簽字串,分為4段, 每段8個位元組;
2)對這四段循環處理, 取8個位元組;
2)對這四段循環處理, 取8個位元組,將他看成16進位串與0x3fffffff(30位1)與操作, 即超過30位的忽略處理;
3)這30位分成6段, 每5位的數字作為字母表的索引取得特定字元, 依序進行取得6位元字串;
4)總的md5字串可以獲得4個6位元字串; 取裡面的任意一個就可作為這個長url的短url位址;
下面是PHP程式碼:
<span>function</span> shorturl(<span>$url</span>='', <span>$prefix</span>='', <span>$suffix</span>=''<span>) {
</span><span>$base32</span> = <span>array</span><span> (
</span>'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'<span>);
</span><span>$hex</span> = <span>md5</span>(<span>$prefix</span>.<span>$url</span>.<span>$suffix</span><span>);
</span><span>$hexLen</span> = <span>strlen</span>(<span>$hex</span><span>);
</span><span>$subHexLen</span> = <span>$hexLen</span> / 8<span>;
</span><span>$output</span> = <span>array</span><span>();
</span><span>for</span> (<span>$i</span> = 0; <span>$i</span> $subHexLen
; $i++) {
$subHex = substr ($hex, $i * 8, 8);
$int = 0x3FFFFFFF & (1 * ('0x'.$subHex));
$out = '';
for ($j = 0; $j $j++) {
$val = 0x0000001F & $int;
$out .= $base32[$val];
$int = $int >> 5;
}
$output[] = $out;
}
return$output;
}
$urls = shorturl('http://www.dareng.com');
var_dump($urls);
以上就介紹了微博短連結演算法php版本,包含了微博短連結的內容,希望對PHP教學有興趣的朋友有幫助。