Sharing of algorithm examples for converting URLs into short URLs using PHP

墨辰丷
Release: 2023-03-29 06:10:02
Original
1727 people have browsed it

Short URL (Short URL), as the name suggests, is a URL that is relatively short in form. In today's Web 2.0 era, I have to say that this is a trend. There are already many similar services. With the help of short URLs, you can replace the original long URLs with short URLs, making it easier for users to share links. Let’s take a look at how to use PHP to implement this function. Friends in need can refer to it. .

Preface

Short URL service may be no longer unfamiliar to many friends. Now it is available in most Weibo, mobile email reminders and other places. There are many application models and occupy a certain market. It is estimated that many friends are using it now.

Benefits of short links:

1. Content needs;
2. User-friendly;
3. Easy to manage.

The following is the algorithm for short URL conversion using PHP. The code is as follows:

PHP

<?php
//短网址生成算法
class ShortUrl {
  
  //字符表
  public static $charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 
  public static function encode($url)
  {
    $key = &#39;abc&#39;; //加盐
    $urlhash = md5($key . $url);
    $len = strlen($urlhash);
 
    //将加密后的串分成4段,每段4字节,对每段进行计算,一共可以生成四组短连接
    for ($i = 0; $i < 4; $i++) {
      $urlhash_piece = substr($urlhash, $i * $len / 4, $len / 4);
      
      //将分段的位与0x3fffffff做位与,0x3fffffff表示二进制数的30个1,即30位以后的加密串都归零
      //此处需要用到hexdec()将16进制字符串转为10进制数值型,否则运算会不正常
      $hex = hexdec($urlhash_piece) & 0x3fffffff;
 
      //域名根据需求填写
      $short_url = "http://t.cn/";
      
      //生成6位短网址
      for ($j = 0; $j < 6; $j++) {
        
        //将得到的值与0x0000003d,3d为61,即charset的坐标最大值
        $short_url .= self::$charset[$hex & 0x0000003d];
        
        //循环完以后将hex右移5位
        $hex = $hex >> 5;
      }
 
      $short_url_list[] = $short_url;
    }
 
    return $short_url_list;
  }
}
 
$url = "http://www.sunbloger.com/";
$short = ShortUrl::encode($url);
print_r($short);
?>
Copy after login

Usually we use the first group of four groups of URLs.

It should be noted here that this algorithm is irreversible. Therefore, the usual approach is to store the short URL and the corresponding original URL in the database. When accessed, the matching original URL is taken out from the database. URL, jump through 301 or header.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php Detailed explanation of PDO exception handling method

PHP reads zip File method

PHP version single sign-in implementation plan

##

The above is the detailed content of Sharing of algorithm examples for converting URLs into short URLs using PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template