zoukankan      html  css  js  c++  java
  • 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 ?>
  • 相关阅读:
    POJ 2427 Smith's Problem Pell方程
    Codeforces Round #194 (Div. 2) 部分题解
    SPOJ 3899. Finding Fractions 连分数
    Codeforces Round #193 (Div. 2) 部分题解
    HDU 1402 A * B Problem Plus FFT
    F的ACM暑期集训计划
    HDU 4607 Park Visit HDU暑期多校1
    Windows 下 Sublime Text 默认打开方式问题解决办法
    Roman To Integer
    Longest Common Prefix
  • 原文地址:https://www.cnblogs.com/liangle/p/3171044.html
Copyright © 2011-2022 走看看