- /**
- * Short join generation algorithm
- * Site: bbs.it-home.org
- */
- Class Short_Url {
- # Orders table
- Public static $ charset = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
-
- Public static function short ($ url ){
- $ Key = "alexis ";
- $ Urlhash = md5 ($ key. $ url );
- $ Len = strlen ($ urlhash );
-
- # Divide the encrypted string into 4 segments, 4 bytes for each segment, and calculate each segment. a total of four short connections can be generated.
- For ($ I = 0; $ I <4; $ I ++ ){
- $ Urlhash_piece = substr ($ urlhash, $ I * $ len/4, $ len/4 );
- # Bitwise and 0x3fffffff are used as bitwise. 0x3fffffff indicates 30 1 of the binary number, that is, the encrypted strings after 30 bits are all set to zero.
- $ Hex = hexdec ($ urlhash_piece) & 0x3fffffff;
-
- $ Short_url = "http://t.cn /";
- # Generate 6-bit short connections
- For ($ j = 0; $ j <6; $ j ++ ){
- # Convert the obtained value to 0x0000003d and 3d to 61, that is, the maximum coordinate value of charset.
- $ Short_url. = self: $ charset [$ hex & 0x0000003d];
- # Remove hex five places to the right after the loop
- $ 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 );
- ?>
Output result: 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 saved to the server and mapped. short_url => original_url. when a short url is entered, it is converted back to the long url based on the ing, and then the original url is accessed. Code:
- Class TinyURL {
- Static private $ key = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; // multiple characters can be properly displayed 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;
- }
- }
Call example:
- $ T = 100;
- $ Time_start = microtime (true );
- While ($ t --){
- Var_dump (TinyURL: encode (1000000 ));
- Var_dump (TinyURL: decode ("4C92 "));
- }
- $ Time_end = microtime (true );
- Printf ("[memory usage: %. 2fMB] \ r \ n", memory_get_usage ()/1024/1024 );
- Printf ("[maximum memory usage: %. 2fMB] \ r \ n", memory_get_peak_usage ()/1024/1024 );
- Printf ("[Execution time: %. 2f millisecond] \ r \ n", ($ time_end-$ time_start) * 1000 );
The above code applies to: traditional relational databases with auto-incremental IDs. You need to execute a secondary SQL statement to obtain the auto-increment ID for the first time and generate a short link based on the ID for the second time. [Or 3 times, an additional time is used to determine whether the short link exists.] In addition, there is also an algorithm that performs Hash operations based on URLs. the advantages of this algorithm are: 1. no id is required, and storage can be satisfied by using a format such as Key/Value. 2. only one statement is required for SQL INSERT. 3. the generated data is discrete and the generation rule cannot be observed. Disadvantages: 1. the Hash algorithm may conflict with each other, and the original one will be overwritten. [Of course, you can add additional logic for judgment.] 2. the data size is difficult to control. you don't know when to start using the new Hash data bit. However, as the data volume increases, the probability of conflict increases. This code is applicable to NoSQL and other non-relational databases, with Fast Search and updates. |