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