Ideas:
1) will be long Web site MD5 generated 32-bit signature string, divided into 4 paragraphs, 8 bytes per paragraph;
2 to the four-segment loop, take 8 bytes, consider him as 16-string and 0X3FFFFFFF (30-bit 1) and operation, that is, more than 30-bit ignore processing;
3 The 30 bits are divided into 6 segments, each 5 digit number is taken as the index of the alphabet to obtain the specific characters, and then the 6-bit string is obtained.
4) The total MD5 string can obtain 4 6-bit strings; Taking any one inside can be used as the short URL of this long URL;
Here is the PHP code:
Copy Code code as follows:
function Shorturl ($url = ', $prefix = ', $suffix = ') {
$base = Array (
' 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 ');
$hex = MD5 ($prefix. $url. $suffix);
$hexLen = strlen ($hex);
$subHexLen = $hexLen/8;
$output = Array ();
for ($i = 0; $i < $subHexLen; $i + +) {
$subHex = substr ($hex, $i * 8, 8);
$int = 0X3FFFFFFF & (1 * (' 0x '. $subHex));
$out = ';
for ($j = 0; $j < 6; $j + +) {
$val = 0x0000001f & $int;
$out. = $base [$val];
$int = $int >> 5;
}
$output [] = $out;
}
return $output;
}
$urls = Shorturl (' http://www.jb51.net/');
Var_dump ($urls);
Results
Copy Code code as follows:
Array (4) {
[0]=>
String (6) "alms1l"
[1]=>
String (6) "2ipmby"
[2]=>
String (6) "Avo1hu"
[3]=>
String (6) "Fdlban"
}
A different version:
Copy Code code as follows:
function Shorturl ($url = ', $prefix = ', $suffix = ') {
$base = Array (
"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",
"6", "7", "8", "9", "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");
$hex = MD5 ($prefix. $url. $suffix);
$hexLen = strlen ($hex);
$subHexLen = $hexLen/8;
$output = Array ();
for ($i = 0; $i < $subHexLen; $i + +) {
$subHex = substr ($hex, $i * 8, 8);
$int = 0X3FFFFFFF & (1 * (' 0x '. $subHex));
$out = ';
for ($j = 0; $j < 6; $j + +) {
$val = 0x0000003d & $int;
$out. = $base [$val];
$int = $int >> 5;
}
$output [] = $out;
}
return $output;
}
Results:
Copy Code code as follows:
Array (4) {
[0] =>
String (6) "6jmMVj"
[1] =>
String (6) "2EnIby"
[2] =>
String (6) "6vIVfu"
[3] =>
String (6) "b7fb6n"
}
But the upgrade version of the collision rate is higher, do not know why.
Test the test code for the collision:
Copy Code code as follows:
$result = Array ();
$repeats = Array ();
$loop = 20000;
for ($i =0; $i < $loop; $i + +) {
$url = ' http://www.jb51.net/?id= '. $i;
$shorta = Shorturl ($url);
$short = $shorta [0];
if (In_array ($short, $result)) {
$repeats [] = $short;
}
$result [] = $short;
}
$result = Array ();
for ($i =0; $i < $loop; $i + +) {
$url = ' http://www.jb51.net/?id= '. $i;
$shorta = Shorturl ($url);
$short = $shorta [0];
if (In_array ($short, $repeats)) {
$result [$short] = $url;
}
}
Var_dump ($repeats);
Var_dump ($result);
Results:
Copy Code code as follows:
Array (8) {
[0] =>
String (6) "3eQBzq"
[1] =>
String (6) "Uqfnay"
[2] =>
String (6) "Qezbiv"
[3] =>
String (6) "Fmneyf"
[4] =>
String (6) "FJJ6FR"
[5] =>
String (6) "3Eviym"
[6] =>
String (6) "J2mmuy"
[7] =>
String (6) "JYQFIV"
}
Array (8) {
' Jyqfiv ' =>
Array (2) {
[0] =>
String (http://www.jb51.net/?id=1640)
[1] =>
String (a) "http://www.jb51.net/?id=18661"
}
' Fmneyf ' =>
Array (2) {
[0] =>
String (http://www.jb51.net/?id=2072)
[1] =>
String (http://www.jb51.net/?id=8480)
}
' 3eQBzq ' =>
Array (2) {
[0] =>
String (http://www.jb51.net/?id=4145)
[1] =>
String (http://www.jb51.net/?id=4273)
}
' J2mmuy ' =>
Array (2) {
[0] =>
String (http://www.jb51.net/?id=7131)
[1] =>
String (a) "http://www.jb51.net/?id=17898"
}
' Qezbiv ' =>
Array (2) {
[0] =>
String (http://www.jb51.net/?id=7320)
[1] =>
String (http://www.jb51.net/?id=8134)
}
' Uqfnay ' =>
Array (2) {
[0] =>
String (http://www.jb51.net/?id=7347)
[1] =>
String (http://www.jb51.net/?id=7962)
}
' Fjj6fr ' =>
Array (2) {
[0] =>
String (http://www.jb51.net/?id=8628)
[1] =>
String (http://www.jb51.net/?id=9031)
}
' 3Eviym ' =>
Array (2) {
[0] =>
String (a) "http://www.jb51.net/?id=11175"
[1] =>
String (a) "http://www.jb51.net/?id=14437"
}
}