Home  >  Article  >  Backend Development  >  PHP short link, short URL, short URL implementation code

PHP short link, short URL, short URL implementation code

WBOY
WBOYOriginal
2016-07-25 08:57:441465browse
  1. /**
  2. * 短连接生成算法
  3. * site: bbs.it-home.org
  4. */
  5. class Short_Url {
  6. #字符表
  7. public static $charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  8. public static function short($url) {
  9. $key = "alexis";
  10. $urlhash = md5($key . $url);
  11. $len = strlen($urlhash);
  12. #将加密后的串分成4段,每段4字节,对每段进行计算,一共可以生成四组短连接
  13. for ($i = 0; $i < 4; $i++) {
  14. $urlhash_piece = substr($urlhash, $i * $len / 4, $len / 4);
  15. #将分段的位与0x3fffffff做位与,0x3fffffff表示二进制数的30个1,即30位以后的加密串都归零
  16. $hex = hexdec($urlhash_piece) & 0x3fffffff; #此处需要用到hexdec()将16进制字符串转为10进制数值型,否则运算会不正常
  17. $short_url = "http://t.cn/";
  18. #生成6位短连接
  19. for ($j = 0; $j < 6; $j++) {
  20. #将得到的值与0x0000003d,3d为61,即charset的坐标最大值
  21. $short_url .= self::$charset[$hex & 0x0000003d];
  22. #循环完以后将hex右移5位
  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. ?>
复制代码

输出结果: Array ( [0] => http://t.cn/KyfLyH [1] => http://t.cn/bPafHS [2] => http://t.cn/H880aD [3] => http://t.cn/TmvDK0 )

生成的短url存到服务器里,做一个映射,short_url => original_url,输入短url的时候按照映射转回长url,然后访问原始url即可。

代码:

  1. Class TinyURL {
  2. static private $key = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //可以多位 保证每位的字符在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. }
复制代码

调用示例:

  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("[内存使用: %.2fMB]\r\n", memory_get_usage() /1024 /1024 );
  9. printf("[内存最高使用: %.2fMB]\r\n", memory_get_peak_usage() /1024 /1024) ;
  10. printf("[执行时间: %.2f毫秒]\r\n", ($time_end - $time_start) * 1000 );
复制代码

The above code is suitable for: Traditional relational database with self-increasing ID. SQL needs to be executed twice, the first time to obtain the auto-increment ID, and the second time to generate a short link based on the ID. [Or 3 times, an additional time is used to determine whether this short link exists. ]

In addition, there is another algorithm that performs Hash operation based on URL. The advantages of this algorithm are: 1. No ID is needed, and the format of Key/Value can be used for storage. 2. SQL insertion requires only one statement. 3. The generated data is discrete and the generation rules cannot be observed.

Disadvantages: 1. All Hash algorithms have the possibility of conflict. Once there is a conflict, the original one will be overwritten. [Of course you can add additional logic to judge. ] 2. The data size is difficult to control. You don’t know when you can start using new Hash data bits, but as the amount of data increases, the probability of conflict will become higher and higher. This kind of code is suitable for non-relational databases such as NoSQL, and it can be searched quickly and updated quickly.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn