PHP short URL

Source: Internet
Author: User
Tags crc32
Two PHP code to implement the short URL function... two methods are provided here

Two PHP codes,
Implement the short URL function ..
Two methods are provided here.
Method 1

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 Functionshorturl ($ input) {$ base62 = array ('A', 'B', 'C', 'D', 'e', 'e', 'F', 'G ', 'H', 'I', 'J', 'K', 'L', 'M', 'n', 'O', 'P', 'Q ', 'R', 'S', 'T', 'u', 'V', 'W', 'X', 'y', 'z', 'A ', 'B', 'C', 'D', 'e', 'F', 'G', 'H', 'I', 'J', 'K ', 'L', 'M', 'n', 'O', 'P', 'Q', 'R', 'S', 'T', 'u ', 'V', 'W', 'X', 'y', 'z', '0', '1', '2', '3', '4 ', '5', '6', '7', '8', '9'); $ hex = md5 ($ input); $ hexLen = strlen ($ hex ); $ subHexLen = $ hexLen/8; $ output = array (); for ($ I = 0; $ I <$ subHexLen; $ I ++) {$ subHex = substr ($ hex, $ I * 8, 8); $ int = 0x3FFFFFFF & (1 * ('0x '. $ subHex); $ out = ''; for ($ j = 0; $ j <6; $ j ++) {$ val = 0x0000003D & $ int; $ out. = $ base62 [$ val]; $ int = $ int> 5;} $ output [] = $ out;} return $ output;} $ ret = signed URL (' http://www.imsiren.com '); Print_r ($ ret );

Method 2:

1234567891011121314151617181920 Functionbase62 ($ x) {$ show = ''; while ($ x> 0) {$ s = $ x % 62; if ($ s> 35) {$ s = chr ($ s + 61);} elseif ($ s> 9 & $ s <= 35) {$ s = chr ($ s + 55 );} $ show. = $ s; $ x = floor ($ x/62);} return $ show;} functionurlShort ($ url) {$ url = crc32 ($ url ); $ res = sprintf ('% u', $ url); returnbase62 ($ res);} echourlShort ("http://www.imsiren.com ");

Method 2: The performance is higher than the crc32 algorithm...
Add to favorites.

 

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.