Weibo short link algorithm php version and Weibo algorithm php. The php version of the Weibo short link algorithm and the php idea of the Weibo algorithm: 1) generate a 32-bit signature string for the long URL md5, which is divided into four sections, each of which is 8 bytes; 2) take 8 bytes for this four-step loop processing and regard him as the php version of the Weibo short link algorithm and the Weibo algorithm php
Ideas:
1) generate a 32-bit signature string for the long URL md5, which is divided into four sections, each of which contains 8 bytes;
2) for these four cycles, take 8 bytes and treat them as hexadecimal strings and 0x3fffffff (30 bits 1) and operations, that is, ignore processing with over 30 bits;
3) The 30 digits are divided into six segments. each of the five digits is used as an index of the alphabet to obtain a specific character, and six strings are obtained in sequence;
4) the total md5 string can be 4 6-bit strings; any one of them can be used as the short url address of this long url;
The following is the PHP code:
function shorturl($url='', $prefix='', $suffix='') { $base32 = 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 .= $base32[$val]; $int = $int >> 5; } $output[] = $out; } return $output;} $urls = shorturl('http://www.dareng.com');var_dump($urls);
Result:
array(4) { [0]=> string(6) "alms1l" [1]=> string(6) "2ipmby" [2]=> string(6) "avo1hu" [3]=> string(6) "fdlban"}
Tips: 1) generate a 32-bit signature string for the long URL md5, which is divided into four sections, each of which is 8 bytes; 2) process these four sections cyclically and take 8 bytes, think of him...