This article mainly introduces the short URL algorithm implemented by php. In theory, it supports 1,073,741,824 short URLs, which are enough for personal use. if you need it, you can refer to each URL and use 6 characters instead, (6 ^ 32) you 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 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