Php short link, short url, short url implementation code-php Tutorial

Source: Internet
Author: User
Php short link, short url, short url implementation code

  1. /**
  2. * Short join generation algorithm
  3. * Site: bbs.it-home.org
  4. */
  5. Class Short_Url {
  6. # Orders table
  7. Public static $ charset = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
  8. Public static function short ($ url ){
  9. $ Key = "alexis ";
  10. $ Urlhash = md5 ($ key. $ url );
  11. $ Len = strlen ($ urlhash );
  12. # 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.
  13. For ($ I = 0; $ I <4; $ I ++ ){
  14. $ Urlhash_piece = substr ($ urlhash, $ I * $ len/4, $ len/4 );
  15. # 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.
  16. $ Hex = hexdec ($ urlhash_piece) & 0x3fffffff;
  17. $ Short_url = "http://t.cn /";
  18. # Generate 6-bit short connections
  19. For ($ j = 0; $ j <6; $ j ++ ){
  20. # Convert the obtained value to 0x0000003d and 3d to 61, that is, the maximum coordinate value of charset.
  21. $ Short_url. = self: $ charset [$ hex & 0x0000003d];
  22. # Remove hex five places to the right after the loop
  23. $ Hex = $ hex> 5;
  24. }
  25. $ Short_url_list [] = $ short_url;
  26. }
  27. Return $ short_url_list;
  28. }
  29. }
  30. $ Url = "http://www.bbs.it-home.org/jb ";
  31. $ Short = Short_Url: short ($ url );
  32. Print_r ($ short );
  33. ?>

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:

  1. Class TinyURL {
  2. Static private $ key = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; // multiple characters can be properly displayed in the URL.
  3. Private function _ construct (){}
  4. Private function _ clone (){}
  5. Static public function encode ($ value ){
  6. $ Base = strlen (self: $ key );
  7. $ Arr = array ();
  8. While ($ value! = 0 ){
  9. $ Arr [] = $ value % $ base;
  10. $ Value = floor ($ value/$ base );
  11. }
  12. $ Result = "";
  13. While (isset ($ arr [0]) $ result. = substr (self: $ key, array_pop ($ arr), 1 );
  14. Return $ result;
  15. }
  16. Static public function decode ($ value ){
  17. $ Base = strlen (self: $ key );
  18. $ Num = 0;
  19. $ Key = array_flip (str_split (self: $ key ));
  20. $ Arr = str_split ($ value );
  21. For ($ len = count ($ arr)-1, $ I = 0; $ I <= $ len; $ I ++ ){
  22. $ Num + = pow ($ base, $ I) * $ key [$ arr [$ len-$ I];
  23. }
  24. Return $ num;
  25. }
  26. }

Call example:

  1. $ T = 100;
  2. $ Time_start = microtime (true );
  3. While ($ t --){
  4. Var_dump (TinyURL: encode (1000000 ));
  5. Var_dump (TinyURL: decode ("4C92 "));
  6. }
  7. $ Time_end = microtime (true );
  8. Printf ("[memory usage: %. 2fMB] \ r \ n", memory_get_usage ()/1024/1024 );
  9. Printf ("[maximum memory usage: %. 2fMB] \ r \ n", memory_get_peak_usage ()/1024/1024 );
  10. 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.

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.