Code that gets the root domain name of the URL in PHP

Source: Internet
Author: User
Tags array count return domain domain name
Copy CodeThe code is as follows:
<?php
/**
* Get root domain
*
* @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;
/**
* Get Domain Name analysis instance
* 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 ', ' being ', ' 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 ', ' GM ', ' 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 ', ' Li ', ' re ', ' Lu ', ' rw ', ' ro ', ' mg ', ' im ', ' mv ', ' Mt ', ' MW ', ' my ', ' ml ', ' mk ', ' MH ', ' MQ ', ' YT ', ' Mu ', ' Mr ', ' US ', ' um ', ' as ', ' VI ', ' mn ', ' Ms ', ' BD ', ' PE ', ' FM ', ' mm ', ' MD ', ' Ma ', ' MC ', ' 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 ', ' SC ', ' sa ', ' CX ', ' st ', ' sh ', ' kn ', ' LC ', ' sm ', ' pm ', ' VC ', ' lk ', ' sk ', ' si ', ' sj ', ' 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 ', ' es ', ' eh ', ' gr ', ' HK ', ' SG ', ' NC ', ' NZ ', ' Hu ', ' Sy ', ' JM ', ' am ', ' ac ', ' ye ', ' iq ', ' ir ', ' il ', ' it ', ' in ', ' id ', ' UK ', ' VG ', ' io ', ' Jo ', ' vn ', ' zm ', ' je ', ' TD ', ' GI ', ' cl ', ' CF ', ' cn ', ' yr '
);
$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;
}
/**
* Get domain Name
* Enter description here ...
*/
Public Function GetDomain () {
return $this->domain;
}
/**
* Get the Host
* Enter description here ...
*/
Public Function GetHost () {
return $this->host;
}
}
?>


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.