[reproduced] Weibo short link algorithm

Source: Internet
Author: User
Tags bitwise

PHP version of the algorithm

Function shorturl ($url = ',  $prefix = ',  $suffix = ')  {     $base 32 =  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  = &NBSP;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 32[$val];              $int  =  $int  >> 5;         }         $output [] =  $out;     }    return  $output;} $urls  = shorturl (' http://www.php100.com '); Var_dump ($urls);

java Edition algorithm

package com.csdn.shorturl;public class shorturlgenerator {     /* *      *  @param  args      */     public static void main (String[] args)  {        //  Long Connection: http://tech.sina.com.cn/i/2011-03-23/11285321288.shtml        //  Sina's post-parse short link is:  http://t.cn/h1jgsc       string  sLongUrl =  "Http://tech.sina.com.cn/i/2011-03-23/11285321288.shtml"  ; //  3bd768e58042156e54626860e241e999       string[] aresult =  shorturl  (Slongurl);       //  print out results         for  ( int i = 0; i < aresult. length ; i + +) &NBSP;{&NBSP;&NBSP;&NBsp;        system. out .println (  "["  + i  +  "]:::"  + aresult[i]);       }     }     public static string[] shorturl (String url)  {        //  can customize build  MD5  encrypt word nginx before mixing  KEY        String key =  "Wuguowei"  ;        //  to use the Generate  URL  character        string[] chars =  new String[] {  "A"  ,  "B"  ,  "C"  ,  "D"  ,  "E" &NBSP;   "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 "&NBSP;   "E"  ,  "F"  ,  "G"  ,  "H"  ,                "I"  ,  "J"  ,  "K"  ,  "L"  ,  "M"  ,  "N"  ,  "O"  ,  "P"  ,  "Q"  ,  "R"  ,  "S"  ,  " T " ,              " U " , " V " , " W "&NBSP;  "X"  ,  "Y"  ,  "Z"         };        //   MD5  encryption for incoming URLs        String  smd5encryptresult =  ( new cmyencrypt ()). GETMD5OFSTR (Key + url);        String hex = sMD5EncryptResult;         String[] resUrl = new String[4];        for  ( int i = 0; i < 4; i++)  {             //  set the encryption character to the  8  bit  16  the binary and   0x3fffffff  for bitwise AND arithmetic            String  Stempsubstring = hex.substring (i * 8, i * 8 + 8);             //  need to use the  long  type to convert, because  inteper .parseint ()   can only handle  31  bits  ,  First sign bit  ,  if LONG&NBSP is not used, it will be out of bounds             long lHexLong = 0x3FFFFFFF & Long.parseLong  (Stempsubstring, &NBSP;16);           string outchars =  " " ;           for  ( int j =  0; j < 6; j++)  {               //  take the resulting value with  0x0000003D  to perform bitwise AND operations, get the character array  chars  index                long index = 0x0000003D  & lhexlong;              //   Add the acquired characters               outchars += chars[( int  )  index];              //   per-loop bitwise right SHIFT  5  bit                lHexLong = lHexLong >> 5;            }           //  storing strings in an output array corresponding to the index            resUrl[i] = outChars;        }       return resUrl;     }}

Jump principle

When we generate short links, we only need to store the mapping of the original link and the short link in the table (database or NoSQL). When we visit a short link, we only need to find the original link from the mapping relationship to jump to the original link.

Reference URL

Http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2012/0606/10506.html

http://blog.csdn.net/wgw335363240/article/details/6568794

[reproduced] Weibo short link algorithm

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.