- /**
- * Short Connection generation algorithm
- * site:bbs.it-home.org
- */
- Class Short_url {
- #字符表
- public static $charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
- public static function short ($url) {
- $key = "Alexis";
- $urlhash = MD5 ($key. $url);
- $len = strlen ($urlhash);
- #将加密后的串分成4段, 4 bytes per segment, calculated for each segment, can generate four short connections altogether
- for ($i = 0; $i < 4; $i + +) {
- $urlhash _piece = substr ($urlhash, $i * $len/4, $len/4);
- #将分段的位与0x3fffffff做位与, 0X3FFFFFFF represents a binary number of 30 1, that is, 30-bit after the encryption string is zeroed
- $hex = Hexdec ($urlhash _piece) & 0x3fffffff; #此处需要用到hexdec () Converts a 16 binary string to a 10-binary numeric type, otherwise the operation will be unhealthy
- $short _url = "http://t.cn/";
- #生成6位短连接
- for ($j = 0; $j < 6; $j + +) {
- #将得到的值与0x0000003d, 3d is 61, which is the coordinate maximum of charset
- $short _url. = self:: $charset [$hex & 0x0000003d];
- #循环完以后将hex右移5位
- $hex = $hex >> 5;
- }
- $short _url_list[] = $short _url;
- }
- return $short _url_list;
- }
- }
- $url = "http://www.bbs.it-home.org/jb//";
- $short = Short_url::short ($url);
- Print_r ($short);
- ?>
Copy CodeOutput: Array ([0] = Http://t.cn/KyfLyH [1] = Http://t.cn/bPafHS [2] = = Http://t.cn/H880aD [3] = = http://t.cn/ TMVDK0) The generated short URL is stored in the server, make a map, Short_url = Original_url, enter the short URL by mapping back to the long URL, and then access the original URL. Code:
- Class TinyURL {
- static Private $key = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; You can make sure that each character is displayed correctly in the URL.
- Private Function __construct () {}
- Private Function __clone () {}
- static public function encode ($value) {
- $base = strlen (self:: $key);
- $arr = Array ();
- while ($value! = 0) {
- $arr [] = $value% $base;
- $value = Floor ($value/$base);
- }
- $result = "";
- while (Isset ($arr [0])) $result. = substr (self:: $key, Array_pop ($arr), 1);
- return $result;
- }
- static public Function decode ($value) {
- $base = strlen (self:: $key);
- $num = 0;
- $key = Array_flip (Str_split (self:: $key));
- $arr = Str_split ($value);
- for ($len = count ($arr)-1, $i = 0; $i <= $len; $i + +) {
- $num + = Pow ($base, $i) * $key [$arr [$len-$i]];
- }
- return $num;
- }
- }
Copy CodeInvocation Example:
- $t = 100;
- $time _start = Microtime (true);
- while ($t-) {
- Var_dump (Tinyurl::encode (1000000));
- Var_dump (tinyurl::d ecode ("4c92"));
- }
- $time _end = Microtime (true);
- printf ("[Memory Use:%.2fmb]\r\n", Memory_get_usage ()/1024/1024);
- printf ("[Maximum Memory Use:%.2fmb]\r\n", Memory_get_peak_usage ()/1024/1024);
- printf ("[Execution time:%.2f Ms]\r\n", ($time _end-$time _start) * 1000);
Copy CodeThe code above applies to: a traditional relational database with a self-increment ID. Need to execute two SQL, first obtained from the increment ID, the second time to generate a short link based on the ID. [or 3 times, an additional time to determine if this short link exists.] ] In addition, there is a hash algorithm based on the URL, the advantages of this algorithm: 1, no need for ID, in the form of key/value to satisfy the storage. 2,sql Insert requires only one statement. 3, the generated data is discrete, unable to observe the rule of generation. Cons: 1, so the hash algorithm has the possibility of conflict, once the conflict is original will be overwritten. [Of course you can add extra logic to judge.] ] 2, the data size is not control, you do not know when to start using the new hash data bits, but as the amount of data increases, the probability of conflict will be higher. This code applies to non-relational databases such as NoSQL, looking for fast updates. |