利用PHP实现短域名互转

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

/**
  * 短域名生成&解析类
  */
 class Build_URL {

     private $mem;
     private $base_url = 'http://xxx.com/';

     public function  __construct() {
         $mem_conf    = array(
                 array(
                         'host'    => '192.168.10.90',
                         'port'    => '11116'
                 ),
                 array(
                         'host'    => '192.168.10.90',
                         'port'    => '11117'
                 ),
         );
         $this->mem    = new Memcache();
         foreach ($mem_conf as $v) {
             $this->mem->addServer($v['host'], $v['port']);
         }
     }

     public function encode($url) {
         $url    = trim($url);
         if(!preg_match("#^[http://|https://|ftp://]#iS", $url)) {
             return false;
         }
         $md5    = md5($url);
         $aid    = $this->mem->get($md5);
         if(!$aid) {
             if(($aid = $this->mem->increment('auto_increment_id')) === false) {
                 $this->mem->set('auto_increment_id', 10000);
                 $aid = $this->mem->increment('auto_increment_id');
             }
             $this->mem->set($md5, $aid);
             $key    = $this->dec2any($aid);
             $this->mem->set($key, $url);
         } else {
             $key    = $this->dec2any($aid);
         }

         return $this->base_url.$key;
     }

     public function decode($url) {
         $key    = str_replace($this->base_url, '', trim($url));
         return $this->mem->get($key);
     }

     private function dec2any($num, $base=62, $index=false) {
         $out = '';
         if (! $base ) {
             $base = strlen($index);
         } else if (! $index ) {
             $index = substr("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,0 ,$base);
         }
         $t = ($num == 0) ? 0 : floor(log10($num) / log10($base));
         for ($t; $t >= 0; $t--) {
             $a = floor($num / pow( $base, $t ));
             $out = $out . substr($index, $a, 1);
             $num = $num - ($a * pow( $base, $t ));
         }
         return $out;
     }
 }

 $app = new Build_URL();
 $url = array(
     'http://www.baidu.com',
     'http://www.google.com',
     '//www.jb51.net'
 );
 foreach ($url as $v) {
     $sort    = $app->encode($v);
     echo "sort link: ".$sort."\n";
     $original    = $app->decode($sort);
     echo "original: ".$original."\n";
 }
 ?>

相关文章

php递归删除目录与文件的方法

本文实例讲述了php递归删除目录与文件的方法。分享给大家供大家参考。具体实现方法如下: <?php function deldir($path){ $dh = ope...

php组合排序简单实现方法

php组合排序简单实现方法

本文实例讲述了php组合排序简单实现方法。分享给大家供大家参考,具体如下: 今天被一个组合排序纠结了一晚上,可能是开始没转过弯,所以没想到用二个栈。用了二个栈就很简单的完成了需求效果...

PHP5.5在windows安装使用memcached服务端的方法

PHP5.5在windows安装使用memcached服务端的方法

  PHP5.5 在windows下安装 memcached 的方法   下载服务端资源   http://download.csdn.net/detail/zsjangel/71047...

如何批量替换相对地址为绝对地址(利用bat批处理实现)

如果你的url链接是相对路径“static/mapi.css”,你想把他批量替换成绝对路径“http://dev.baidu.com/wiki/static/map/cloud/stat...

PHP实现的多进程控制demo示例

本文实例讲述了PHP实现的多进程控制。分享给大家供大家参考,具体如下: 自己写了个多进程控制的框架代码,留着备查 declare(ticks=1); function sigHand...