PHP generates short URL
Principle:
1. Verify the original URL with crc32 and get the check code.
2. Use sprintf('%u') to convert the check code into an unsigned number.
3. Perform a remainder 62 operation on unsigned numbers (uppercase and lowercase letters + numbers equal 62 bits), map the remainder to 62 characters, and save the mapped characters. (For example, the remainder is 10, then the mapped character is A, 0-9 corresponds to 0-9, 10-35 corresponds to A-Z, 35-62 corresponds to a-z)
4. Loop until the value is 0.
5. Splice all the mapped characters together to get the code after the short URL.
The code is as follows:
Copy code The code is as follows:
/**Generate short URL
* @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.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/726024.htmlTechArticlephp Principle of generating short URL: 1. Perform crc32 verification on the original URL to get the check code. 2. Use sprintf('%u') to convert the check code into an unsigned number. 3. Perform remainder 62 operation on unsigned numbers (large...