PHP Generate short URL principle and code _php tutorial

Source: Internet
Author: User
Tags crc32 sprintf
PHP generates short URLs

Principle:

1. CRC32 Check the original URL, get the check code.

2. Use sprintf ('%u ') officer to convert to unsigned digits.

3. For the unsigned number of the remainder 62 operation (uppercase and lowercase letters + numbers equal to 62 bits), to obtain a remnant after mapping to 62 characters, the mapped characters are saved. (for example, the remainder is 10, the mapped characters are a,0-9 corresponding to the 0-9,10-35 corresponding to the a-z,35-62 corresponding to A-Z)

4. Loop the operation until the value is 0.

5. Stitch all the mapped characters, which is the code after the short URL.

The code is as follows:
Copy CodeThe code is as follows:
/** Generate short URLs
* @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.html www.bkjia.com true http://www.bkjia.com/PHPjc/726024.html techarticle PHP generated Short URL principle: 1. The original URL to do CRC32 check, get the check code. 2. Use sprintf ('%u ') officer to convert to unsigned digits. 3. To the unsigned number for the remainder 62 operation (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.