利用PHP實現短域名互轉
如何使用PHP實現短域名互轉?下面的代碼可以幫助你實現,非常簡單,需要的朋友可以參考下。
代碼如下:
/**
* 短域名生成&解析類
*/
class Build_URL {
private $mem;
private $base_url = '';
public function __construct() {
$mem_conf = array(
array(
'host' => '',
'port' => '11116'
),
array(
'host' => '',
'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(
'',
'',
''
);
foreach ($url as $v) {
$sort = $app->encode($v);
echo "sort link: ".$sort."n";
$original = $app->decode($sort);
echo "original: ".$original."n";
}
?>
-
如何在php與js中分別獲取客户度mac地址
導語:如何在php與js中分別獲取客户度mac地址的呢?學代碼的你懂嗎?下面的是本站小編為大家蒐集的`相關資料,供大家參考。代碼如下:<?phpclassMacAddr{public$returnArray=array();public$macAddr;function__contruct($os_type=null){if(is_null($os_type))$os_type=...
-
tp搜索時首頁分頁和搜索頁保持條件分頁的方法
在做搜索查詢時突然發現在首頁用的`分頁代碼在搜索頁使用時出現錯誤,首頁分頁代碼(代碼中標註start與end部分為分頁代碼),下面是由本站小編為大家整理的Thinkphp搜索時首頁分頁和搜索頁保持條件分頁的方法,喜歡的可以收藏一下!瞭解更多詳情資訊,請關注應屆畢業生...
-
PHP正則匹配中文字母數字正則的表達式
PHP語言是一門實用性很強的語言,下面小編為大家帶來了關於PHP正則匹配中文字母數字正則的'表達式,歡迎大家閲讀!PHP正則匹配中文字母數字正則的表達式代碼如下if(preg_match("/^d*$/","4312")){echo"全數字";}if(preg_match("/^[a-z]*$/i","fdsFDfd")){echo"全...
-
解決PHPutf-8編碼問題的方法
PHPutf-8編碼問題,着是困惑很多人的問題,如果處理不好,都是亂碼,下面是解決PHPutf-8編碼問題的方法,一起來學習下吧:1.在建數據庫的時候,尤其是用phpMyAdmin與MYSQL打交道時候,一般都是utf-8,字段為utf8_general_ci數據庫的設置:在文件中查找:[mysql]default-character-s...