• PHP 实现短域名互转


     1 /**
     2  * 短域名生成&解析类
     3  */
     4 class Build_URL {
     5 
     6     private $mem;
     7     private $base_url = 'http://xxx.com/';
     8 
     9     public function  __construct() {
    10         $mem_conf    = array(
    11                 array(
    12                         'host'    => '192.168.10.90',
    13                         'port'    => '11116'
    14                 ),
    15                 array(
    16                         'host'    => '192.168.10.90',
    17                         'port'    => '11117'
    18                 ),
    19         );
    20         $this->mem    = new Memcache();
    21         foreach ($mem_conf as $v) {
    22             $this->mem->addServer($v['host'], $v['port']);
    23         }
    24     }
    25 
    26     public function encode($url) {
    27         $url    = trim($url);
    28         if(!preg_match("#^[http://|https://|ftp://]#iS", $url)) {
    29             return false;
    30         }
    31         $md5    = md5($url);
    32         $aid    = $this->mem->get($md5);
    33         if(!$aid) {
    34             if(($aid = $this->mem->increment('auto_increment_id')) === false) {
    35                 $this->mem->set('auto_increment_id', 10000);
    36                 $aid = $this->mem->increment('auto_increment_id');
    37             }
    38             $this->mem->set($md5, $aid);
    39             $key    = $this->dec2any($aid);
    40             $this->mem->set($key, $url);
    41         } else {
    42             $key    = $this->dec2any($aid);
    43         }
    44 
    45         return $this->base_url.$key;
    46     }
    47 
    48     public function decode($url) {
    49         $key    = str_replace($this->base_url, '', trim($url));
    50         return $this->mem->get($key);
    51     }
    52 
    53     private function dec2any($num, $base=62, $index=false) {
    54         $out = '';
    55         if (! $base ) {
    56             $base = strlen($index);
    57         } else if (! $index ) {
    58             $index = substr("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,0 ,$base);
    59         }
    60         $t = ($num == 0) ? 0 : floor(log10($num) / log10($base));
    61         for ($t; $t >= 0; $t--) {
    62             $a = floor($num / pow( $base, $t ));
    63             $out = $out . substr($index, $a, 1);
    64             $num = $num - ($a * pow( $base, $t ));
    65         }
    66         return $out;
    67     }
    68 }
    69 
    70 $app = new Build_URL();
    71 $url = array(
    72     'http://www.baidu.com',
    73     'http://www.google.com',
    74     'http://edu.cnblogs.com'
    75 );
    76 foreach ($url as $v) {
    77     $sort    = $app->encode($v);
    78     echo "sort link: ".$sort."
    ";
    79     $original    = $app->decode($sort);
    80     echo "original: ".$original."
    ";
    81 }
    82 ?>
  • 相关阅读:
    构造函数语义学之Default Constructor构建操作
    c++子类继承父类的覆盖问题
    C++中自己理解的一些细节哈
    学习C++所需看的书和顺序
    C++中强制变换之const_cast
    jquery锚点跳转
    关于iPhone X 适配
    input图片上传并显示查看判断图片类型
    jquery操作按钮修改对应input属性
    织梦dedecms会员中心分类管理无法修改、删除分类名
  • 原文地址:https://www.cnblogs.com/liangle/p/3171044.html
Copyright © 2020-2023  润新知