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.
Copy the Code code as follows:
function Shorturl ($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 32[$val];
$int = $int >> 5;
}
$output [] = $out;
}
return $output;
}
Test code:
Copy the Code code as follows:
$input = ' HTTP://WWW.JB51.NET/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.JB51.NET/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:
Copy the Code code as follows:
Input:http://www.jb51.net/1
output:h0xg4r
Bdr3tw
Osk2d3
4azfqa
Input:http://www.jb51.net/2
Output:tm5kxb
Ceoj2s
Yw3dvl
Nrmrxl
http://www.bkjia.com/PHPjc/825414.html www.bkjia.com true http://www.bkjia.com/PHPjc/825414.html techarticle 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's true ...