Php url Shortening code (generating short URLs) _ PHP Tutorial

Source: Internet
Author: User
Shorten the php url code (generate a short URL ). The short website is now used more and more jump websites will be generated, such as the original Weibo also has this function, next, I would like to recommend a PHP URL Shortening code. each URL uses 6 characters to replace the short url. Currently, many jump websites are generated, this function is also available on weibo. I would like to recommend a PHP URL Shortening code for you.

Each URL is replaced by 6 characters, and (32 ^ 6) 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 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;

}


Sample code to test/use the above function:

$ Input = 'http: // www. bKjia. c0m/1 ';

$ Output = response URL ($ input );


Echo "Input: $ inputn ";

Echo "Output: {$ output [0]} n ";

Echo "{$ output [1]} n ";

Echo "{$ output [2]} n ";

Echo "{$ output [3]} n ";

Echo "n ";


$ Input = 'http: // www. bKjia. c0m/2 ';

$ Output = response URL ($ input );


Echo "Input: $ inputn ";

Echo "Output: {$ output [0]} n ";

Echo "{$ output [1]} n ";

Echo "{$ output [2]} n ";

Echo "{$ output [3]} n ";

Echo "n ";


Output:

Http://www.bKjia. c0m/1

Output: h0xg4r

Bdr3tw

Osk2d3

4 azfqa


Http://www.bKjia. c0m/2

Output: tm5kxb

Ceoj2s

Yw3dvl

Nrmrxl

Each URL is replaced by 6 characters...

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.