Weibo short link algorithm php version, Weibo algorithm php_PHP tutorial

Source: Internet
Author: User
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...

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.