Short web site services, may be a lot of friends are no longer unfamiliar, and now most of the microblogging, mobile mail reminders and other places have a lot of application mode, and occupy a certain market. It is estimated that many friends are using it now. Read Sina's short connection service, found behind the main 6 string composition.
Too many algorithmic things, there is no need to explore too much, the most important or implementation, the following are three methods of code:
<?php//Pure random generation method function random ($length, $pool = ') {$random = ';
if (empty ($pool)) {$pool = ' abcdefghkmnpqrstuvwxyz ';
$pool. = ' 23456789 ';
Srand (Double) microtime () *1000000);
for ($i = 0; $i < $length; $i + +) {$random. = substr ($pool, (rand ()% (strlen ($pool)), 1);
return $random;
} $a =random (6);
Print_r ($a); Enumeration Generation method function Shorturl ($input) {$base = Array ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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" "," 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 ");
$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;
} $a =shorturl ("http://www.jb51.net");
Print_r ($a);
62-bit Generation method function Base62 ($x) {$show = ';
while ($x > 0) {$s = $x% 62;
if ($s >) {$s = Chr ($s +61);
} elseif ($s > 9 && $s <=35) {$s = Chr ($s + 55);
$show. = $s;
$x = Floor ($x/62);
return $show;
The function Urlshort ($url) {$url = CRC32 ($url);
$result = sprintf ("%u", $url);
Return base62 ($result);
Echo urlshort ("http://www.jb51.net/");
?>