PHP generates short URLs
Principle:
1. CRC32 Check the original URL, get the check code.
2. Use sprintf ('%u ') officer to convert to unsigned digits.
3. For the unsigned number of the remainder 62 operation (uppercase and lowercase letters + numbers equal to 62 bits), to obtain a remnant after mapping to 62 characters, the mapped characters are saved. (for example, the remainder is 10, the mapped characters are a,0-9 corresponding to the 0-9,10-35 corresponding to the a-z,35-62 corresponding to A-Z)
4. Loop the operation until the value is 0.
5. Stitch all the mapped characters, which is the code after the short URL.
The code is as follows:
Copy CodeThe code is as follows:
/** Generate short URLs
* @param String $url Original URL
* @return String
*/
function DWZ ($url) {
$code = sprintf ('%u ', CRC32 ($url));
$surl = ";
while ($code) {
$mod = $code% 62;
if ($mod >9 && $mod <=35) {
$mod = Chr ($mod + 55);
}elseif ($mod >35) {
$mod = Chr ($mod + 61);
}
$surl. = $mod;
$code = Floor ($code/62);
}
return $surl;
}
http://www.bkjia.com/PHPjc/726024.html www.bkjia.com true http://www.bkjia.com/PHPjc/726024.html techarticle PHP generated Short URL principle: 1. The original URL to do CRC32 check, get the check code. 2. Use sprintf ('%u ') officer to convert to unsigned digits. 3. To the unsigned number for the remainder 62 operation (Large ...