Home > php教程 > php手册 > body text

PHP短域名互转的实例分享

WBOY
Release: 2016-06-13 11:28:55
Original
1218 people have browsed it

代码如下:

<?<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>?>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template