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;
} Sample code to Test/use the above function:
$input = ' HTTP://WWW.BKJIA.C0M/1 '; $output = Shorturl ($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 = Shorturl ($input); echo "Input: $inputn";
echo "Output: {$output [0]}n]; echo "{$output [1]}n]; echo "{$output [2]}n]; echo "{$output [3]}n]; echo "n"; Output:
Input:http://www.bkjia.c0m/1 output:h0xg4r Bdr3tw Osk2d3 4azfqa Input:http://www.bkjia.c0m/2
Output:tm5kxb Ceoj2s Yw3dvl Nrmrxl |