熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

利用PHP實現短域名互轉

2013-11-15 12:38:12  來源: PHP編程 
如何使用PHP實現短域名互轉?下面的代碼可以幫助你實現非常簡單需要的朋友可以參考下   復制代碼 代碼如下:

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

     private $mem;
     private $base_url = ;

     public function  __construct() {
         $mem_conf    = array(
                 array(
                         host    =>
                         port    =>
                 )
                 array(
                         host    =>
                         port    =>
                 )
         );
         $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;
         }
         $md    = md($url);
         $aid    = $this>mem>get($md);
         if(!$aid) {
             if(($aid = $this>mem>increment(auto_increment_id)) === false) {
                 $this>mem>set(auto_increment_id );
                 $aid = $this>mem>increment(auto_increment_id);
             }
             $this>mem>set($md $aid);
             $key    = $this>decany($aid);
             $this>mem>set($key $url);
         } else {
             $key    = $this>decany($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 decany($num $base= $index=false) {
         $out = ;
         if (! $base ) {
             $base = strlen($index);
         } else if (! $index ) {
             $index = substr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" $base);
         }
         $t = ($num == ) ? : floor(log($num) / log($base));
         for ($t; $t >= ; $t) {
             $a = floor($num / pow( $base $t ));
             $out = $out substr($index $a );
             $num = $num ($a * pow( $base $t ));
         }
         return $out;
     }
 }

 $app = new Build_URL();
 $url = array(
    
    
    
 );
 foreach ($url as $v) {
     $sort    = $app>encode($v);
     echo "sort link: "$sort"n";
     $original    = $app>decode($sort);
     echo "original: "$original"n";
 }
 ?>


From:http://tw.wingwit.com/Article/program/PHP/201311/21330.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.