列出几个网上收集的关于利用php脚本获取顶级域名的方法,可能分析的不是很完全,大家可以参考一下,适当的做一些调整。

php获取顶级域名的方法

示例1:

function url_root($url=""){
    $url = $url ? $url : $_SERVER["HTTP_HOST"];
    $dual_host = array('aaa.pro','ac.cn','ac.kr','ac.mu','aca.pro','acct.pro','ae.org','ah.cn','ar.com','avocat.pro','bar.pro','biz.ki','biz.pl','bj.cn','br.com','busan.kr','chungbuk.kr','chungnam.kr','club.tw','cn.com','co.ag','co.am','co.at','co.bz','co.cm','co.com','co.gg','co.gl','co.gy','co.il','co.im','co.in','co.je','co.kr','co.lc','co.mg','co.ms','co.mu','co.nl','co.nz','co.uk','co.ve','co.za','com.af','com.ag','com.am','com.ar','com.au','com.br','com.bz','com.cm','com.cn','com.co','com.de','com.ec','com.es','com.gl','com.gr','com.gy','com.hn','com.ht','com.im','com.ki','com.lc','com.lv','com.mg','com.ms','com.mu','com.mx','com.nf','com.pe','com.ph','com.pk','com.pl','com.ps','com.pt','com.ro','com.ru','com.sb','com.sc','com.se','com.sg','com.so','com.tw','com.vc','com.ve','cpa.pro','cq.cn','daegu.kr','daejeon.kr','de.com','ebiz.tw','edu.cn','edu.gl','eng.pro','es.kr','eu.com','fin.ec','firm.in','fj.cn','game.tw','gangwon.kr','gb.com','gb.net','gd.cn','gen.in','go.kr','gov.cn','gr.com','gs.cn','gwangju.kr','gx.cn','gyeongbuk.kr','gyeonggi.kr','gyeongnam.kr','gz.cn','ha.cn','hb.cn','he.cn','hi.cn','hk.cn','hl.cn','hn.cn','hs.kr','hu.com','hu.net','idv.tw','in.net','incheon.kr','ind.in','info.ec','info.ht','info.ki','info.nf','info.pl','info.ve','jeju.kr','jeonbuk.kr','jeonnam.kr','jl.cn','jp.net','jpn.com','js.cn','jur.pro','jx.cn','kg.kr','kiwi.nz','kr.com','law.pro','ln.cn','me.uk','med.ec','med.pro','mex.com','mo.cn','ms.kr','ne.kr','net.af','net.ag','net.am','net.br','net.bz','net.cm','net.cn','net.co','net.ec','net.gg','net.gl','net.gr','net.gy','net.hn','net.ht','net.im','net.in','net.je','net.ki','net.lc','net.lv','net.mg','net.mu','net.my','net.nf','net.nz','net.ph','net.pk','net.pl','net.ps','net.ru','net.sb','net.sc','net.so','net.vc','net.ve','nm.cn','no.com','nom.ag','nom.co','nom.es','nom.ro','nx.cn','or.at','or.jp','or.kr','or.mu','org.af','org.ag','org.am','org.bz','org.cn','org.es','org.gg','org.gl','org.gr','org.hn','org.ht','org.il','org.im','org.in','org.je','org.ki','org.lc','org.lv','org.mg','org.ms','org.mu','org.my','org.nz','org.pk','org.pl','org.ps','org.ro','org.ru','org.sb','org.sc','org.so','org.uk','org.vc','org.ve','pe.kr','pro.ec','qc.com','qh.cn','radio.am','radio.fm','re.kr','recht.pro','ru.com','sa.com','sc.cn','sc.kr','sd.cn','se.com','senet','seoul.kr','sh.cn','sn.cn','sx.cn','tj.cn','tw.cn','uk.com','uk.net','ulsan.kr','us.com','us.org','uy.com','web.ve','xj.cn','xz.cn','yn.cn','za.com','zj.cn'); 
    $url_arr   = explode(".", $url);
    if (count($url_arr) <= 2) {
        $host = $url;
    } else {
        $last   = array_pop($url_arr);
        $last_1 = array_pop($url_arr);
        $last_2 = array_pop($url_arr);
        $host   = $last_1.'.'.$last;
        if (in_array($host, $dual_host)) {
            $host = $last_2.'.'.$last_1.'.'.$last;
        }
    }
    return $host;
}

示例2:

function getDoMain($url)
{
    if(empty($url)){
        return '';
    }
    
    if(strpos($url,'http://') !== false){
        $url = str_replace('http://','',$url);
    }
    
    if(strpos($url,'https://') !== false){
        $url = str_replace('https://','',$url);
    }
    
    $n = 0;
    for($i = 1;$i <= 3;$i++) {
        $n = strpos($url, '/', $n);
        $i != 3 && $n++;
    }
    
    $nn = strpos($url, '?');
    $mix_num = min($n,$nn);
    if($mix_num > 0 || !empty($mix_num)){ 
        $url = mb_substr($url,0,$mix_num);
    }
    
    $data = explode('.', $url);
    $co_ta = count($data);
    
    //判断是否是双后缀
    
    $no_tow = true;
    $host_cn = 'com.cn,net.cn,org.cn,gov.cn';
    $host_cn = explode(',', $host_cn);
    foreach($host_cn as $val){
        if(strpos($url,$val)){
            $no_tow = false;
        }
    }
    
    //截取域名后的目录
    $del = strpos($data[$co_ta-1], '/');
    if($del > 0 || !empty($del)){
        $data[$co_ta-1] = mb_substr($data[$co_ta-1],0,$del);
    }
    
    //如果是返回FALSE ,如果不是返回true
    if($no_tow == true){
        $host = $data[$co_ta-2].'.'.$data[$co_ta-1];
    }else{
        $host = $data[$co_ta-3].'.'.$data[$co_ta-2].'.'.$data[$co_ta-1];
    }
    return $host;
}

示例3:

function get_host($to_virify_url = '')
{    
    $url   = $to_virify_url ? $to_virify_url : $_SERVER['HTTP_HOST'];
    $data = explode('.', $url);
    $co_ta = count($data);
    $zi_tow = true;
    $host_cn = 'com.cn,net.cn,org.cn,gov.cn';
    $host_cn = explode(',', $host_cn);
    foreach($host_cn as $host){
        if(strpos($url,$host)){
            $zi_tow = false;
        }
    }
    if($zi_tow == true){
        if($url == 'localhost'){
            $host = $data[$co_ta-1];
        }else{
            $host = $data[$co_ta-2].'.'.$data[$co_ta-1];
        }   
    }else{
        $host = $data[$co_ta-3].'.'.$data[$co_ta-2].'.'.$data[$co_ta-1];
    }    
    return $host;
}