短网址服务,可能很多朋友都已经不再陌生,现在大部分微博、手机邮件提醒等地方已经有很多应用模式了,并占据了一定的市场。估计很多朋友现在也正在使用。 看过新浪的短连接服务,发现后面主要有6个字符串组成。

太多算法的东西,也没必要去探讨太多,最主要的还是实现,下面是三种方法的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<span style="color: #000000;font-weight: bold"><?php</span> 
 
 
 
<span style="color: #666666;font-style: italic">//纯随机生成方法</span>
<span style="color: #000000;font-weight: bold">function</span> random<span style="color: #009900">(</span><span style="color: #000088">$length</span><span style="color: #339933">,</span> <span style="color: #000088">$pool</span> <span style="color: #339933">=</span> <span style="color: #0000ff">''</span><span style="color: #009900">)</span> 
    <span style="color: #009900">{</span> 
        <span style="color: #000088">$random</span> <span style="color: #339933">=</span> <span style="color: #0000ff">''</span><span style="color: #339933">;</span> 
 
        <span style="color: #b1b100">if</span> <span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/empty" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">empty</span></a><span style="color: #009900">(</span><span style="color: #000088">$pool</span><span style="color: #009900">)</span><span style="color: #009900">)</span> <span style="color: #009900">{</span> 
            <span style="color: #000088">$pool</span>    <span style="color: #339933">=</span> <span style="color: #0000ff">'abcdefghkmnpqrstuvwxyz'</span><span style="color: #339933">;</span> 
            <span style="color: #000088">$pool</span>   <span style="color: #339933">.=</span> <span style="color: #0000ff">'23456789'</span><span style="color: #339933">;</span> 
        <span style="color: #009900">}</span> 
 
        <a rel="noopener noreferrer nofollow" href="http://www.php.net/srand" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">srand</span></a> <span style="color: #009900">(</span><span style="color: #009900">(</span>double<span style="color: #009900">)</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/microtime" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">microtime</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">*</span><span style="color: #cc66cc">1000000</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
 
        <span style="color: #b1b100">for</span><span style="color: #009900">(</span><span style="color: #000088">$i</span> <span style="color: #339933">=</span> <span style="color: #cc66cc">0</span><span style="color: #339933">;</span> <span style="color: #000088">$i</span> <span style="color: #339933"><</span> <span style="color: #000088">$length</span><span style="color: #339933">;</span> <span style="color: #000088">$i</span><span style="color: #339933">++</span><span style="color: #009900">)</span>  
        <span style="color: #009900">{</span> 
            <span style="color: #000088">$random</span> <span style="color: #339933">.=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/substr" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">substr</span></a><span style="color: #009900">(</span><span style="color: #000088">$pool</span><span style="color: #339933">,</span><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/rand" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">rand</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">%</span><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/strlen" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">strlen</span></a> <span style="color: #009900">(</span><span style="color: #000088">$pool</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">,</span> <span style="color: #cc66cc">1</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
        <span style="color: #009900">}</span> 
 
        <span style="color: #b1b100">return</span> <span style="color: #000088">$random</span><span style="color: #339933">;</span> 
    <span style="color: #009900">}</span> 
 
 <span style="color: #000088">$a</span><span style="color: #339933">=</span>random<span style="color: #009900">(</span><span style="color: #cc66cc">6</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/print_r" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">print_r</span></a><span style="color: #009900">(</span><span style="color: #000088">$a</span><span style="color: #009900">)</span><span style="color: #339933">;</span>   
 
<span style="color: #666666;font-style: italic">// 枚举生成方法</span>
<span style="color: #000000;font-weight: bold">function</span> shorturl<span style="color: #009900">(</span><span style="color: #000088">$input</span><span style="color: #009900">)</span> <span style="color: #009900">{</span> 
  <span style="color: #000088">$base32</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a> <span style="color: #009900">(</span> 
    <span style="color: #0000ff">"0"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"1"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"2"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"3"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"4"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"5"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"6"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"7"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"8"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"9"</span><span style="color: #339933">,</span>    
<span style="color: #0000ff">"a"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"b"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"c"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"d"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"e"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"f"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"g"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"h"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"i"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"j"</span><span style="color: #339933">,</span>   
<span style="color: #0000ff">"k"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"l"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"m"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"n"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"o"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"p"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"q"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"r"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"s"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"t"</span><span style="color: #339933">,</span>   
<span style="color: #0000ff">"u"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"v"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"w"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"x"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"y"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"z"</span><span style="color: #339933">,</span>    
<span style="color: #0000ff">"A"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"B"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"C"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"D"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"E"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"F"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"G"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"H"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"I"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"J"</span><span style="color: #339933">,</span>    
<span style="color: #0000ff">"K"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"L"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"M"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"N"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"O"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"P"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"Q"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"R"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"S"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"T"</span><span style="color: #339933">,</span>    
 <span style="color: #0000ff">"U"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"V"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"W"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"X"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"Y"</span><span style="color: #339933">,</span> <span style="color: #0000ff">"Z"</span>
    <span style="color: #009900">)</span><span style="color: #339933">;</span> 
 
  <span style="color: #000088">$hex</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/md5" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">md5</span></a><span style="color: #009900">(</span><span style="color: #000088">$input</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
  <span style="color: #000088">$hexLen</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/strlen" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">strlen</span></a><span style="color: #009900">(</span><span style="color: #000088">$hex</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
  <span style="color: #000088">$subHexLen</span> <span style="color: #339933">=</span> <span style="color: #000088">$hexLen</span> <span style="color: #339933">/</span> <span style="color: #cc66cc">8</span><span style="color: #339933">;</span> 
  <span style="color: #000088">$output</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
 
  <span style="color: #b1b100">for</span> <span style="color: #009900">(</span><span style="color: #000088">$i</span> <span style="color: #339933">=</span> <span style="color: #cc66cc">0</span><span style="color: #339933">;</span> <span style="color: #000088">$i</span> <span style="color: #339933"><</span> <span style="color: #000088">$subHexLen</span><span style="color: #339933">;</span> <span style="color: #000088">$i</span><span style="color: #339933">++</span><span style="color: #009900">)</span> <span style="color: #009900">{</span> 
    <span style="color: #000088">$subHex</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/substr" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">substr</span></a> <span style="color: #009900">(</span><span style="color: #000088">$hex</span><span style="color: #339933">,</span> <span style="color: #000088">$i</span> <span style="color: #339933">*</span> <span style="color: #cc66cc">8</span><span style="color: #339933">,</span> <span style="color: #cc66cc">8</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
    <span style="color: #000088">$int</span> <span style="color: #339933">=</span> <span style="color: #208080">0x3FFFFFFF</span> <span style="color: #339933">&</span> <span style="color: #009900">(</span><span style="color: #cc66cc">1</span> <span style="color: #339933">*</span> <span style="color: #009900">(</span><span style="color: #0000ff">'0x'</span><span style="color: #339933">.</span><span style="color: #000088">$subHex</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
    <span style="color: #000088">$out</span> <span style="color: #339933">=</span> <span style="color: #0000ff">''</span><span style="color: #339933">;</span> 
 
    <span style="color: #b1b100">for</span> <span style="color: #009900">(</span><span style="color: #000088">$j</span> <span style="color: #339933">=</span> <span style="color: #cc66cc">0</span><span style="color: #339933">;</span> <span style="color: #000088">$j</span> <span style="color: #339933"><</span> <span style="color: #cc66cc">6</span><span style="color: #339933">;</span> <span style="color: #000088">$j</span><span style="color: #339933">++</span><span style="color: #009900">)</span> <span style="color: #009900">{</span> 
      <span style="color: #000088">$val</span> <span style="color: #339933">=</span> <span style="color: #208080">0x0000001F</span> <span style="color: #339933">&</span> <span style="color: #000088">$int</span><span style="color: #339933">;</span> 
      <span style="color: #000088">$out</span> <span style="color: #339933">.=</span> <span style="color: #000088">$base32</span><span style="color: #009900">[</span><span style="color: #000088">$val</span><span style="color: #009900">]</span><span style="color: #339933">;</span> 
      <span style="color: #000088">$int</span> <span style="color: #339933">=</span> <span style="color: #000088">$int</span> <span style="color: #339933">>></span> <span style="color: #cc66cc">5</span><span style="color: #339933">;</span> 
    <span style="color: #009900">}</span> 
 
    <span style="color: #000088">$output</span><span style="color: #009900">[</span><span style="color: #009900">]</span> <span style="color: #339933">=</span> <span style="color: #000088">$out</span><span style="color: #339933">;</span> 
  <span style="color: #009900">}</span> 
 
  <span style="color: #b1b100">return</span> <span style="color: #000088">$output</span><span style="color: #339933">;</span> 
<span style="color: #009900">}</span> 
<span style="color: #000088">$a</span><span style="color: #339933">=</span>shorturl<span style="color: #009900">(</span><span style="color: #0000ff">"http://www.chhua.com"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/print_r" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">print_r</span></a><span style="color: #009900">(</span><span style="color: #000088">$a</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #666666;font-style: italic">//62 位生成方法</span>
 
<span style="color: #000000;font-weight: bold">function</span> base62<span style="color: #009900">(</span><span style="color: #000088">$x</span><span style="color: #009900">)</span> 
 
<span style="color: #009900">{</span> 
 
<span style="color: #000088">$show</span><span style="color: #339933">=</span> <span style="color: #0000ff">''</span><span style="color: #339933">;</span> 
 
 <span style="color: #b1b100">while</span><span style="color: #009900">(</span><span style="color: #000088">$x</span><span style="color: #339933">></span> <span style="color: #cc66cc">0</span><span style="color: #009900">)</span> <span style="color: #009900">{</span> 
 
<span style="color: #000088">$s</span><span style="color: #339933">=</span> <span style="color: #000088">$x</span><span style="color: #339933">%</span> <span style="color: #cc66cc">62</span><span style="color: #339933">;</span> 
 
<span style="color: #b1b100">if</span><span style="color: #009900">(</span><span style="color: #000088">$s</span><span style="color: #339933">></span> <span style="color: #cc66cc">35</span><span style="color: #009900">)</span> <span style="color: #009900">{</span> 
 
<span style="color: #000088">$s</span><span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/chr" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">chr</span></a><span style="color: #009900">(</span><span style="color: #000088">$s</span><span style="color: #339933">+</span><span style="color: #cc66cc">61</span><span style="color: #009900">)</span><span style="color: #339933">;</span>             
 
<span style="color: #009900">}</span> <span style="color: #b1b100">elseif</span><span style="color: #009900">(</span><span style="color: #000088">$s</span><span style="color: #339933">></span> <span style="color: #cc66cc">9</span> <span style="color: #339933">&&</span> <span style="color: #000088">$s</span><span style="color: #339933"><=</span><span style="color: #cc66cc">35</span><span style="color: #009900">)</span> <span style="color: #009900">{</span> 
 
<span style="color: #000088">$s</span><span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/chr" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">chr</span></a><span style="color: #009900">(</span><span style="color: #000088">$s</span><span style="color: #339933">+</span> <span style="color: #cc66cc">55</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
 
<span style="color: #009900">}</span> 
 
<span style="color: #000088">$show</span><span style="color: #339933">.=</span> <span style="color: #000088">$s</span><span style="color: #339933">;</span> 
 
 <span style="color: #000088">$x</span><span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/floor" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">floor</span></a><span style="color: #009900">(</span><span style="color: #000088">$x</span><span style="color: #339933">/</span><span style="color: #cc66cc">62</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
 
<span style="color: #009900">}</span> 
 
<span style="color: #b1b100">return</span> <span style="color: #000088">$show</span><span style="color: #339933">;</span>     
 
<span style="color: #009900">}</span> 
 
<span style="color: #000000;font-weight: bold">function</span> urlShort<span style="color: #009900">(</span><span style="color: #000088">$url</span><span style="color: #009900">)</span> 
 
<span style="color: #009900">{</span> 
 
<span style="color: #000088">$url</span><span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/crc32" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">crc32</span></a><span style="color: #009900">(</span><span style="color: #000088">$url</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
 
<span style="color: #000088">$result</span><span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/sprintf" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">sprintf</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"<span style="color: #009933;font-weight: bold">%u</span>"</span><span style="color: #339933">,</span> <span style="color: #000088">$url</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
 
<span style="color: #b1b100">return</span> base62<span style="color: #009900">(</span><span style="color: #000088">$result</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
 
 <span style="color: #009900">}</span> 
 
<span style="color: #b1b100">echo</span> urlShort<span style="color: #009900">(</span><span style="color: #0000ff">"http://www.chhua.com/"</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
 
<span style="color: #000000;font-weight: bold">?></span>