Php obtains the code of the root domain name of the URL. For more information, see.
The code is as follows:
/**
* Get the root domain name
*
* @ Author lonely
* @ Create 2011-3-11
* @ Version 0.1
* @ Lastupdate lonely
* @ Package Sl
*/
Class Sl_RootDomain {
Private static $ self;
Private $ domain = null;
Private $ host = null;
Private $ state_domain;
Private $ top_domain;
/**
* Retrieve domain name analysis instances
* Enter description here...
*/
Public static function instace (){
If (! Self: $ self)
Self: $ self = new self ();
Return self: $ self;
}
Private function _ construct (){
$ This-> state_domain = array (
'Al', 'dz', 'AF', 'ar ',' AE ', 'aw', 'Om', 'Az ', 'EG', 'ET ', 'Ie', 'ee ', 'AD', 'ao', 'Ai', 'AG', 'at', 'Au', 'Mo', 'BB ', 'PG ', 'BS', 'PK', 'py', 'PS ', 'Bh', 'Pa ', 'Br', 'By', 'bm ', 'bg ', 'MP', 'BJ', 'be', 'is ', 'PR', 'ba', 'pl', 'Bo', 'BZ ', 'bw ', 'BT', 'BF', 'bi', 'bv ', 'KP', 'GQ', 'dk ', 'De', 'Tl ', 'TP ', 'tg', 'DM', 'Do ', 'Ru', 'EC ', 'ER', 'Fr ', 'Fo', 'pf ', 'GF ', 'tf', 'VA', 'Ph ', 'fj', 'Fi ', 'CV', 'fk ', 'G', 'cg ', 'CD', 'Co', 'cr ', 'GG', 'gd ', 'Gl', 'GE ', 'CU', 'GP ', 'Gu ', 'gy', 'kz', 'ht ', 'Kr', 'nl ', 'an', 'hm ', 'hn', 'Ki ', 'DJ ', 'KG ', 'Gn', 'gw ', 'CA', 'GH', 'GA', 'kh', 'cz', 'zw', 'CM ', 'qa ', 'ky', 'km ', 'ci', 'kw', 'CC', 'HR ', 'ke', 'CK ', 'LV ', 'Ls', 'LA', 'lb', 'Lt ', 'LR', 'ly ', 'Lil', 're', 'Lu', 'rw ', 'ro', 'MG ', 'Im', 'mmv ', 'mt', 'mw', 'my', 'M', 'MK ', 'MH ', 'mq ', 'yt', 'Mu', 'Mr ', 'us', 'um', 'as', 'VI', 'mn ', 'Ms ', 'bd ', 'PE', 'FM', 'mm', 'MD', 'M', 'mz', 'MX ', 'nr ', 'NP ', 'ni', 'ne', 'NG ', 'Nu', 'no', 'NF ', 'Na', 'Za', 'Aq ', 'GS ', 'EU', 'pw ', 'pn', 'pt ', 'JP', 'se', 'CH', 'sv', 'Ws ', 'yu ', 'sl', 'SN ', 'cy', 'scs', 'sa ', 'cx', 'st', 'sh', 'kn ', 'LC ', 'SM', 'PM ', 'VC', 'LK ', 'SK', 'Si', 'S1', 'sz', 'SD ', 'sr', 'sb ', 'so', 'TJ', 'tw ', 'th', 'tz', 'to', 'TC ', 'TT ', 'tn ',' TV ', 'tr', 'TM', 'TK ', 'WF', 'vu ', 'GT', 've ', 'bn ', 'ug', 'ua ', 'uy', 'uz', 'els', 'eh', 'Gr ', 'HK', 'SG ', 'NC ', 'yz', 'hu', 'sy ', 'jm', 'AM ', 'AC', 'Ye', 'IQ ', 'ir', 'Il ', 'It', 'in', 'id', 'uk', 'vg ', 'io', 'Jo', 'vn', 'zm', 'Je ', 'TD ', 'GI', 'cl', 'CF ', 'cn', 'ymr'
);
$ This-> top_domain = array ('com ', 'arpa', 'edu', 'Gov', 'int', 'Mil ', 'net', 'org ', 'biz', 'info', 'pro', 'name', 'Museum ', 'coop', 'Aero', 'XXX', 'idv', 'me ', 'mobi ');
$ This-> url = $ _ SERVER ['http _ host'];
}
/**
* Set URL
* Enter description here...
* @ Param string $ url
*/
Public function setUrl ($ url = null ){
$ Url = $ url? $ Url: $ this-> url;
If (empty ($ url) return $ this;
If (! Preg_match ("/^ http:/is", $ url ))
$ Url = "http: //". $ url;
$ Url = parse_url (strtolower ($ url ));
$ Urlarr = explode (".", $ url ['host']);
$ Count = count ($ urlarr );
If ($ count <= 2 ){
$ This-> domain = array_pop ($ url );
} Else if ($ count> 2 ){
$ Last = array_pop ($ urlarr );
$ Last_1 = array_pop ($ urlarr );
If (in_array ($ last, $ this-> top_domain )){
$ This-> domain = $ last_1. '.'. $ last;
$ This-> host = implode ('.', $ urlarr );
} Else if (in_array ($ last, $ this-> state_domain )){
$ Last_2 = array_pop ($ urlarr );
If (in_array ($ last_1, $ this-> top_domain )){
$ This-> domain = $ last_2. '.'. $ last_1. '.'. $ last;
$ This-> host = implode ('.', $ urlarr );
} Else {
$ This-> host = implode ('.', $ urlarr). $ last_2;
$ This-> domain = $ last_1. '.'. $ last;
}
}
}
Return $ this;
}
/**
* Retrieve domain names
* Enter description here...
*/
Public function getDomain (){
Return $ this-> domain;
}
/**
* Obtain the host
* Enter description here...
*/
Public function getHost (){
Return $ this-> host;
}
}
?>