Principles and code of generating short URLs using php. Principles of generating short URLs in php: 1. perform crc32 verification on the original website to obtain the verification code. 2. use sprintf (% u) to convert the verification code to an unsigned number. 3. perform the remaining 62 operations on the unsigned number (large php generates short URLs)
Principle:
1. perform crc32 verification on the original website to obtain the verification code.
2. use sprintf ('% u') to convert the verification code to an unsigned number.
3. perform the remainder 62 operation on the unsigned number (uppercase and lowercase letters + digits are equal to 62 digits). after the remainder is obtained, it is mapped to 62 characters and the mapped characters are saved. (For example, if the remainder is 10, the ing character is A, 0-9 corresponds to 0-9, 10-35 corresponds to the A-Z, 35-62 corresponds to a-z)
4. perform cyclic operations until the value is 0.
5. concatenate all ing characters, that is, the code after the short URL.
The code is as follows:
The code is as follows:
/** Generate a short URL
* @ Param String $ 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 to generate a short web site principle: 1. the original Web site crc32 verification, get the verification code. 2. use sprintf ('% u') to convert the verification code to an unsigned number. 3. perform the remainder 62 operation on the unsigned number (large...