Php url shortening code (generate short URL)

Source: Internet
Author: User

The short website is now used more and more jump websites will be generated, like the original Weibo also has this function, I would like to recommend you a PHP URL shortening code

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: Copy code

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

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.