Weibo short link algorithm PHP version implementation code _php tutorial

Source: Internet
Author: User
Ideas:
1) to generate a long URL MD5 32-bit signature string, divided into 4 segments, 8 bytes per segment;
2) for this four-segment cycle processing, take 8 bytes, he is regarded as 16 binary string and 0X3FFFFFFF (30 bit 1) and operation, that is, more than 30 bits of ignoring processing;
3) The 30 bits are divided into 6 segments, and each 5 digit number is taken as an index of the alphabet to obtain a specific character, followed by a 6-bit string;
4) The total MD5 string can obtain 4 6-bit strings; Take any one inside to be the short URL address for this long URL;
Here is the PHP code:
Copy CodeThe code is 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 CodeThe code is as follows:
Array (4) {
[0]=>
String (6) "alms1l"
[1]=>
String (6) "2ipmby"
[2]=>
String (6) "Avo1hu"
[3]=>
String (6) "Fdlban"
}

A different version:
Copy CodeThe code is 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 CodeThe code is 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 even higher, do not know why.
Test code for testing collisions:
Copy CodeThe code is 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 CodeThe code is 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 ("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 ("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 ("http://www.jb51.net/?id=11175")
[1] = =
String ("http://www.jb51.net/?id=14437")
}
}

http://www.bkjia.com/PHPjc/325982.html www.bkjia.com true http://www.bkjia.com/PHPjc/325982.html techarticle ideas: 1) The long URL MD5 generated 32-bit signature string, divided into 4 segments, 8 bytes per segment; 2) for this four-segment loop processing, take 8 bytes, and treat him as 16 binary string with 0X3FFFFFFF (30 bit 1) with operation, ie over ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.