Php code for obtaining the root domain name of a URL

Source: Internet
Author: User
Php obtains the code of the root domain name of the URL. For more information, see.

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;
}
}
?>
Related Article

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.