Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn a sample code for generating a short URL in php.
How php generates short urls:
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.
Example 1: php short URL implementation code.
Sample Code:
/** 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;
}
Example of generating a short URL:
Sample Code:
Echo dwz ('HTTP: // www.heimaolianmeng.com '); // lVRl4
?>
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB