Php code for obtaining the root domain name of a URL _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php obtains the code of the root domain name of the URL. Copy the code as follows :? Php *** get the root domain ** @ authorlonely * @ create2011-3-11 * @ version0.1 * @ lastupdatelonely * @ packageSl * classSl_RootDomain {privatestati 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;
}
}
?>

The http://www.bkjia.com/PHPjc/323040.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323040.htmlTechArticle code is as follows :? Php/*** get the root domain ** @ author lonely * @ create 2011-3-11 * @ version 0.1 * @ lastupdate lonely * @ package Sl */class Sl_RootDomain {private stati...

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.