PHP implementation URL Long connection generate short link operation

Source: Internet
Author: User
Short link, in layman's terms, is to convert long URL urls into short URL strings through program calculations.

In this case, the advantages are: 1, content needs, 2, user-friendly, 3, easy to manage.

Early short links are widely used in image uploading sites, reducing the number of URLs by reducing the number of URLs, to reduce the purpose of code strings. More convenient for users to reference URLs, write code, "save" the number of characters in space. Common in the use of online image classification, because of the character restrictions, the use of short links, to achieve the purpose of the external chain picture. Since the popularity of Weibo, in the limited number of micro-BO features, short links are also prevalent in the microblogging site, in order to save words, to the bloggers to publish more text space.

<?php/** * Short link operation generated by long connections * Algorithm Description: Use 6 characters to denote short links, we use the ASCII character of ' a '-' z ', ' 0 '-' 9 ', ' a '-' Z ', a total of 62 characters as a set. * Each character has 62 states, six characters can represent 62^6 (56800235584), then how to get these six characters, * specifically described as follows: * 1. Set the key value of the passed long url+ to Md5, get a 32-bit string (32 character hexadecimal number), that is 16 32 times; * 2. Divide the 32 bits into four parts, each 8 characters, as 16 binary strings and 0X3FFFFFFF (30 bit 1) and operation, i.e., more than 30 bits of ignoring processing; * 3. The 30 bits are divided into 6 segments, each of the 5 groups, the integer value is calculated, and then mapped to the 62 characters we prepared, sequentially to get a 6-bit short link address.    * * @author flyer0126 * @since 2012/07/13 */function shorturl ($long _url) {$key = ' flyer0126 ';        $base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";    The hash value is generated using the MD5 algorithm $hex = hash (' MD5 ', $long _url $key);    $hexLen = strlen ($hex);        $subHexLen = $hexLen/8;    $output = Array (); for ($i = 0; $i < $subHexLen; $i + +) {//Divide the 32 bits into four parts, 8 characters each, and treat them as 16 strings with 0X3FFFFFFF (30 bit 1) and Operation $subHex = substr ($he    X, $i *8, 8);       $idx = 0X3FFFFFFF & (1 * (' 0x '. $subHex));    The 30 bits are divided into 6 segments, each of the 5 groups, and the integer values are calculated, and then mapped to the 62 characters we prepared $out = "; for ($j = 0; $j < 6; $j + +) {$vaL = 0x0000003d & $idx;    $out. = $base 32[$val];    $idx = $idx >> 5;    } $output [$i] = $out;  } return $output;} $url = ' http://flyer0126.iteye.com/'; $ret = Shorturl ($url); Var_dump ($ret) ################ printing results ################/* Array (4) {[0]=>string (6) "2aEzqe" [1]=>string (6) "Rj6bve" [2]=>string (6) "F2mqvi" [3]=>string (6) "Z2eqyv"} */

#将url与短链接结果存入数据库, take one of them for display.

  • Related Article

    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.