PHP短域名互转的实例分享_PHP教程

WBOY
发布: 2016-07-20 11:13:03
原创
933 人浏览过

代码如下:

<?<span php
</span><span /*</span><span *
* 短域名生成&解析类
* edit www.jbxue.com
</span><span */</span>
<span class</span><span  Build_URL {

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

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

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

</span><span return</span> <span $this</span>->base_url.<span $key</span><span ;
}

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

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

</span><span $app</span> = <span new</span><span  Build_URL();
</span><span $url</span> = <span array</span><span (
</span>'http://www.baidu.com',
'http://www.google.com',
'http://www.jbxue.com'<span 
);
</span><span foreach</span> (<span $url</span> <span as</span> <span $v</span><span ) {
</span><span $sort</span> = <span $app</span>->encode(<span $v</span><span );
</span><span echo</span> "sort link: ".<span $sort</span>."\n"<span ;
</span><span $original</span> = <span $app</span>->decode(<span $sort</span><span );
</span><span echo</span> "original: ".<span $original</span>."\n"<span ;
}
</span>?>
登录后复制

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440417.htmlTechArticle代码如下: ? = 'http://xxx.com/' = 'host' = '192.168.10.90','port' = '11116' , 'host' = '192.168.10.90','port' = '11117' , -mem = ( -mem-addServer( ['host'], ['port' encode( =...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!