PHP Short URL Algorithm example code sharing _php Tutorial

Source: Internet
Author: User
PHP implements a short URL algorithm that theoretically supports 1,073,741,824 short URLs.

Each URL is replaced by 6 characters, and (6^32) can have a maximum of 1,073,741,824 short URLs.
Of course, you can also record more detailed information such as access history, creation time, etc.
If it is not enough, it can also be deleted for a long time.

functionShorturl ($input) {$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($input);$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;}

Test code:

  $input  = ' http://www.jbxue.com/1 ' ;   $output  = Shorturl ( $input  );   Echo  "Input:  $input  \ n" ;   Echo  "Output: { $output  [0]}\n" ;   Echo  "{ $output  [1]}\n" ;   Echo  "{ $output  [2]}\n" ;   Echo  "{ $output  [3]}\n" ;   Echo  "\ n" ;   $input  = ' http://www.jbxue.com/2 ' ;   $output  = Shorturl ( $input  );   Echo  "Input:  $input  \ n" ;   Echo  "Output: { $output  [0]}\n" ;   Echo  "{ $output  [1]}\n" ;   Echo  "{ $output  [2]}\n" ;   Echo  "{ $output  [3]}\n" ;   Echo  "\ n"; 

Output:

Input:http://www.jbxue.com/1
output:h0xg4r
Bdr3tw
Osk2d3
4azfqa

Input:http://www.jbxue.com/2
Output:tm5kxb
Ceoj2s
Yw3dvl
Nrmrxl

Articles you may be interested in:

    • Principles and examples of short URLs generated by PHP
    • PHP generates short URL sample code
    • The idea and implementation of PHP generating short URLs
    • PHP Generate short URL imitation microblogging short URL generation code
    • PHP short URL algorithm php generated short URL implementation code
    • PHP short URL Super simple code
    • Simple code for PHP to generate short URLs
    • A sample code for PHP to generate a short URL
    • PHP Short URL Implementation code

http://www.bkjia.com/PHPjc/815970.html www.bkjia.com true http://www.bkjia.com/PHPjc/815970.html techarticle PHP implements a short URL algorithm that theoretically supports 1,073,741,824 short URLs. Each URL is replaced by 6 characters, and (6^32) can have a maximum of 1,073,741,824 short URLs. Of course, you can also remember ...

  • 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.