PHP Short link algorithm collection and analysis _php skills

Source: Internet
Author: User
Tags md5
Short links do not say, we have been clear, as shown below is a short link:
Sina Micro Bo Http://t.cn/SVpONM
Tencent Micro Bo Http://url.cn/302yor
Yun.io http://d.yun.io/PNri2v
Advantages of short Links: 1, Content needs 2, user-friendly 3, easy to manage.
How to achieve it, there are about three steps:
1, the definition of a URL mapping algorithm, you can map the long URL into a short string;
2. Use one storage (database?) Nosql? ) to store the completed mappings;
3, the realization of their own URL mapping algorithm;
In general, the third step is a headache for us, how to map a long URL string into a shorter string. I have summed up three ways:
General implementation
I think we've all learned about decimal and binary conversions, or decimal and hexadecimal conversions, so for shorter, we can use 62 to convert a numeric ID to a short string.
The disadvantage of this approach is that there is no way to ensure that all links are fixed in the length of the number of digits, and in the case of high concurrency, how to ensure rapid distribution is a problem.
Specific implementation methods:
Copy Code code as follows:

/**
* Use 62 to encode the digital ID short link, the disadvantage does not guarantee that each short link is fixed length
*
* @author wanshiqiang<wangshiqiang@360.cn>
* @param integer $integer
* @param string $base
*/
Private Function Getshortenedurlfromid ($integer, $base = allowed_chars)
{
$length = strlen ($base);
while ($integer > $length-1)
{
$out = $base [Fmod ($integer, $length)]. $out;
$integer = Floor ($integer/$length);
}
return $base [$integer]. $out;
}
/**
* Decode 62-encoded short links
*
* @author wangshiqiang<wangshiqiang@360.cn>
* @param string $string
* @param string $base
*/
Private Function Getidfromshortenedurl ($string, $base = allowed_chars)
{
$length = strlen ($base);
$size = strlen ($string)-1;
$string = Str_split ($string);
$out = Strpos ($base, Array_pop ($string));
foreach ($string as $i => $char)
{
$out + + strpos ($base, $char) * POW ($length, $size-$i);
}
return $out;
}

Literary and artistic realization
Algorithm Description: Using 6 characters to represent short links, we use the ASCII character of ' a '-' z ', ' 0 '-' 5 ', a total of 32 characters as a set. Each character has 32 states, and six characters can represent 32^6 (1073741824), then how to get the six characters, described below:
The incoming long URL is Md5 to get a 32-bit string that changes a lot, is 16 32 times, and can basically guarantee uniqueness. Divide these 32 bits into four parts, each 8 characters, then the probability becomes 16 8 times, is 4294967296, this number collision probability is also relatively small, the key is the later processing. We'll think of this 8-bit character as a 16-digit integer, that is, 1* (' 0x '. $val), then take 0-30 bits, every 5 groups, work out his integer value, then map to the 32 characters we have prepared, and finally we can get a 6-bit short link address.
PHP implementation is as follows:
Copy Code code as follows:

Function Shorten ($long _url)
{
$base = "abcdefghijklmnopqrstuvwxyz012345";
$hex = MD5 ($long _url);
$hexLen = strlen ($hex);
$subHexLen = $hexLen/8;
$output = Array ();
for ($i = 0; $i < $subHexLen; $i + +)
{
$subHex = substr ($hex, $i * 8, 8);
$subHex = 0X3FFFFFFF & (1 * (' 0x '. $subHex));
$out = ';
for ($j = 0; $j < 6; $j + +)
{
$val = 0x0000001f & $int;
$out. = $base 32[$val];
$int = $int >> 5;
}
$output [] = $out;
}
return $output;
}

The second force realizes
The following function uses a purely random way to generate a short link, although we can make sure that the short link is not reused by querying the operation, but ... Is this really true?
Copy Code code as follows:

function random ($length, $pool = ' ") {
$random = ';
if (empty ($pool)) {$pool = ' abcdefghkmnpqrstuvwxyz '; = $pool. =
' 23456789 '; }
Srand (Double) microtime () *1000000);
for ($i = 0; $i < $length; $i + +) {$random. =
SUBSTR ($pool, (rand ()% (strlen ($pool)), 1); }
return $random;
}

Technorati Tags: short links, shorter URLs, maps, hashes

Resources:

1, Micro Bo short Address principle analysis

2, micro-bo short domain name principle and function

3, yours.org

4, free PHP URL Shorten script that kicks ass

5, PHP short URL algorithm implementation

6, implement your own short URL

7, Short URL algorithm preliminary summary

8, Short URL implementation method

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.