Php code for generating short URLs

Source: Internet
Author: User
Tags crc32
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.