Using PHP to implement mutual transfer of short domain names _ PHP Tutorial

Source: Internet
Author: User
Use PHP to implement mutual transfer of short domain names. Copy the code as follows: *** generate nbsp for a short domain name; * classBuild_URL {private $ mem; private $ base_urlxxx.com; publicfunction _ construct () {$ mem_confarray ( The code is as follows:


/**
* Short domain name generation and resolution
*/
Class Build_URL {

Private $ mem;
Private $ base_url = 'http: // xxx.com /';

Public function _ construct (){
$ Mem_conf = array (
Array (
'Host' => '2014. 168.10.90 ',
'Port' => '123'
),
Array (
'Host' => '2014. 168.10.90 ',
'Port' => '123'
),
);
$ 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( "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 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 (
'Http: // www.baidu.com ',
'Http: // www.google.com ',
'Http: // www.jb51.net'
);
Foreach ($ url as $ v ){
$ Sort = $ app-> encode ($ v );
Echo "sort link:". $ sort. "\ n ";
$ Original = $ app-> decode ($ sort );
Echo "original:". $ original. "\ n ";
}
?>

The http://www.bkjia.com/PHPjc/328002.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/328002.htmlTechArticle code is as follows:/*** short domain name generation nbsp; */class Build_URL {private $ mem; private $ base_url = 'http: // xxx.com/'; public function _ construct () {$ mem_conf = array (...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.