Maison > php教程 > php手册 > le corps du texte

PHP短域名互转的实例分享

WBOY
Libérer: 2016-06-13 11:28:55
original
1219 Les gens l'ont consulté

代码如下:

<?<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>?>
Copier après la connexion
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal