思路:
1)将长网址md5生成32位签名串,分为4段, 每段8个字节;
2)对这四段循环处理, 取8个字节, 将他看成16进制串与0x3fffffff(30位1)与操作, 即超过30位的忽略处理;
3)这30位分成6段, 每5位的数字作为字母表的索引取得特定字符, 依次进行获得6位字符串;
4)总的md5串可以获得4个6位串; 取里面的任意一个就可作为这个长url的短url地址;
下面是PHP代码:
<span style="color: #0000ff;">function</span> shorturl(<span style="color: #800080;">$url</span>='', <span style="color: #800080;">$prefix</span>='', <span style="color: #800080;">$suffix</span>=''<span style="color: #000000;">) { </span><span style="color: #800080;">$base32</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;"> ( </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 style="color: #000000;">); </span><span style="color: #800080;">$hex</span> = <span style="color: #008080;">md5</span>(<span style="color: #800080;">$prefix</span>.<span style="color: #800080;">$url</span>.<span style="color: #800080;">$suffix</span><span style="color: #000000;">); </span><span style="color: #800080;">$hexLen</span> = <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$hex</span><span style="color: #000000;">); </span><span style="color: #800080;">$subHexLen</span> = <span style="color: #800080;">$hexLen</span> / 8<span style="color: #000000;">; </span><span style="color: #800080;">$output</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $subHexLen; <span style="color: #800080;">$i</span>++<span style="color: #000000;">) { </span><span style="color: #800080;">$subHex</span> = <span style="color: #008080;">substr</span> (<span style="color: #800080;">$hex</span>, <span style="color: #800080;">$i</span> * 8, 8<span style="color: #000000;">); </span><span style="color: #800080;">$int</span> = 0x3FFFFFFF & (1 * ('0x'.<span style="color: #800080;">$subHex</span><span style="color: #000000;">)); </span><span style="color: #800080;">$out</span> = ''<span style="color: #000000;">; </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$j</span> = 0; <span style="color: #800080;">$j</span> $j++<span style="color: #000000;">) { </span><span style="color: #800080;">$val</span> = 0x0000001F & <span style="color: #800080;">$int</span><span style="color: #000000;">; </span><span style="color: #800080;">$out</span> .= <span style="color: #800080;">$base32</span>[<span style="color: #800080;">$val</span><span style="color: #000000;">]; </span><span style="color: #800080;">$int</span> = <span style="color: #800080;">$int</span> >> 5<span style="color: #000000;">; } </span><span style="color: #800080;">$output</span>[] = <span style="color: #800080;">$out</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$output</span><span style="color: #000000;">; } </span><span style="color: #800080;">$urls</span> = shorturl('<a href="http://www.dareng.com">http://www.dareng.com</a>'<span style="color: #000000;">); </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$urls</span>);
结果:
<span style="color: #0000ff;">array</span>(4<span style="color: #000000;">) { [</span>0]=> <span style="color: #0000ff;">string</span>(6) "alms1l"<span style="color: #000000;"> [</span>1]=> <span style="color: #0000ff;">string</span>(6) "2ipmby"<span style="color: #000000;"> [</span>2]=> <span style="color: #0000ff;">string</span>(6) "avo1hu"<span style="color: #000000;"> [</span>3]=> <span style="color: #0000ff;">string</span>(6) "fdlban"<span style="color: #000000;"> }</span>