Short URL algorithm implemented by php _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Share short URL algorithms implemented by php. Each URL is replaced by 6 characters, and (6 ^ 32) can have up to 1,073,741,824 short URLs. Of course, you can also record more detailed information, such as access records and creation time. If each URL is replaced by six characters, (6 ^ 32) a maximum of 1,073,741,824 short URLs can be created.
Of course, you can also record more detailed information, such as access records and creation time.
If it is not enough, you can delete it for a long time.

The code is as follows:


Function compute URL ($ input ){
$ Base32 = array (
'A', 'B', 'C', 'D', 'e', '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. = $ base32 [$ val];
$ Int = $ int> 5;
}

$ Output [] = $ out;
}

Return $ output;
}

Test code:

The code is as follows:


$ Input = 'http: // www.jb51.net/1 ';
$ Output = response URL ($ 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.jb51.net/2 ';
$ Output = response URL ($ 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:

The code is as follows:


Input: http://www.jb51.net/1
Output: h0xg4r
Bdr3tw
Osk2d3
4 azfqa

Input: http://www.jb51.net/2
Output: tm5kxb
Ceoj2s
Yw3dvl
Nrmrxl

Expires, (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 records and creation time. If it is true...

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.