How php generates short URLs and code _ PHP Tutorial

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

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.