Few months ago, I introduced a simple algorithmthat allow users to implement their own short URL into their system. Today,I have some spare time so I decided to write the short URL algorithm'simplementation in PHP.
At first, we define a function called shorturl() thatreceives a URL as the input and returns anarray that contains 4 hashed values (each 6 characters).
function shorturl($input) { ... // return array of results}
Below is the original pseudocode:
... loop2: from 1st 4 bytes to 4th 4 bytes of md5 result cast the 4 bytes to an integer loop3: for shortCodeChar[0] to shortCodeChar[5] use 1st 5 bits of the integer to find the value in codeMap remove 5 bits from the integer end loop3 save shortCodeChar as shortCode ... // Database checking for duplication end loop2...The following code is written according to thealgorithm above excluding the database checking part for duplication:
[php] function shorturl($input) { $base32 = 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 .= $base32[$val]; $int = $int >> 5; } $output[] = $out; } return $output; } function shorturl($input) {
$base32 = 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 .= $base32[$val];
$int = $int >> 5;
}
$output[] = $out;
}
return $output;
}Sample code to test/use theabove function:
[php] $input = 'http://www.snippetit.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.snippetit.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"; $input = 'http://www.snippetit.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.snippetit.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:
[plain] Input : http://www.snippetit.com/1 Output : h0xg4r bdr3tw osk2d3 4azfqa Input : http://www.snippetit.com/2 Output : tm5kxb ceoj2s yw3dvl nrmrxl Input : http://www.snippetit.com/1
Output : h0xg4r
bdr3tw
osk2d3
4azfqa
Input : http://www.snippetit.com/2
Output : tm5kxb
ceoj2s
yw3dvl
nrmrxlThe function return an array of 4 elements, youcan use any one of them. The others can be us