PHP implements extracting root domain name through URL

不言
Release: 2023-04-01 12:42:01
Original
3760 people have browsed it

This article shares with you what I encountered when I was working on a project. I need to use PHP to extract the root domain name from the URL and the implementation ideas. Friends in need can refer to

PHP Extract the root domain name based on the URL. I use it in my personal work. Since many codes on the Internet cannot get the correct results, I wrote one myself. Everyone is welcome to use it and report bugs.

<?php 
#使用示例
echo getBaseDomain(&#39;http://blog.jp.goo.ne.jp/index.php&#39;)->domain;echo "\n";
echo getBaseDomain(&#39;http://51.ca/index.php&#39;)->domain;echo "\n";
echo getBaseDomain(&#39;http://blog.ab.cc.win.aisa.hk/index.php&#39;)->domain;echo "\n";
 
 
function getBaseDomain($url=&#39;&#39;){
  if(!$url){
    return $url;
  }
  #列举域名中固定元素
  $state_domain = array(
    &#39;al&#39;,&#39;dz&#39;,&#39;af&#39;,&#39;ar&#39;,&#39;ae&#39;,&#39;aw&#39;,&#39;om&#39;,&#39;az&#39;,&#39;eg&#39;,&#39;et&#39;,&#39;ie&#39;,&#39;ee&#39;,&#39;ad&#39;,&#39;ao&#39;,&#39;ai&#39;,&#39;ag&#39;,&#39;at&#39;,&#39;au&#39;,&#39;mo&#39;,&#39;bb&#39;,&#39;pg&#39;,&#39;bs&#39;,&#39;pk&#39;,&#39;py&#39;,&#39;ps&#39;,&#39;bh&#39;,&#39;pa&#39;,&#39;br&#39;,&#39;by&#39;,&#39;bm&#39;,&#39;bg&#39;,&#39;mp&#39;,&#39;bj&#39;,&#39;be&#39;,&#39;is&#39;,&#39;pr&#39;,&#39;ba&#39;,&#39;pl&#39;,&#39;bo&#39;,&#39;bz&#39;,&#39;bw&#39;,&#39;bt&#39;,&#39;bf&#39;,&#39;bi&#39;,&#39;bv&#39;,&#39;kp&#39;,&#39;gq&#39;,&#39;dk&#39;,&#39;de&#39;,&#39;tl&#39;,&#39;tp&#39;,&#39;tg&#39;,&#39;dm&#39;,&#39;do&#39;,&#39;ru&#39;,&#39;ec&#39;,&#39;er&#39;,&#39;fr&#39;,&#39;fo&#39;,&#39;pf&#39;,&#39;gf&#39;,&#39;tf&#39;,&#39;va&#39;,&#39;ph&#39;,&#39;fj&#39;,&#39;fi&#39;,&#39;cv&#39;,&#39;fk&#39;,&#39;gm&#39;,&#39;cg&#39;,&#39;cd&#39;,&#39;co&#39;,&#39;cr&#39;,&#39;gg&#39;,&#39;gd&#39;,&#39;gl&#39;,&#39;ge&#39;,&#39;cu&#39;,&#39;gp&#39;,&#39;gu&#39;,&#39;gy&#39;,&#39;kz&#39;,&#39;ht&#39;,&#39;kr&#39;,&#39;nl&#39;,&#39;an&#39;,&#39;hm&#39;,&#39;hn&#39;,&#39;ki&#39;,&#39;dj&#39;,&#39;kg&#39;,&#39;gn&#39;,&#39;gw&#39;,&#39;ca&#39;,&#39;gh&#39;,&#39;ga&#39;,&#39;kh&#39;,&#39;cz&#39;,&#39;zw&#39;,&#39;cm&#39;,&#39;qa&#39;,&#39;ky&#39;,&#39;km&#39;,&#39;ci&#39;,&#39;kw&#39;,&#39;cc&#39;,&#39;hr&#39;,&#39;ke&#39;,&#39;ck&#39;,&#39;lv&#39;,&#39;ls&#39;,&#39;la&#39;,&#39;lb&#39;,&#39;lt&#39;,&#39;lr&#39;,&#39;ly&#39;,&#39;li&#39;,&#39;re&#39;,&#39;lu&#39;,&#39;rw&#39;,&#39;ro&#39;,&#39;mg&#39;,&#39;im&#39;,&#39;mv&#39;,&#39;mt&#39;,&#39;mw&#39;,&#39;my&#39;,&#39;ml&#39;,&#39;mk&#39;,&#39;mh&#39;,&#39;mq&#39;,&#39;yt&#39;,&#39;mu&#39;,&#39;mr&#39;,&#39;us&#39;,&#39;um&#39;,&#39;as&#39;,&#39;vi&#39;,&#39;mn&#39;,&#39;ms&#39;,&#39;bd&#39;,&#39;pe&#39;,&#39;fm&#39;,&#39;mm&#39;,&#39;md&#39;,&#39;ma&#39;,&#39;mc&#39;,&#39;mz&#39;,&#39;mx&#39;,&#39;nr&#39;,&#39;np&#39;,&#39;ni&#39;,&#39;ne&#39;,&#39;ng&#39;,&#39;nu&#39;,&#39;no&#39;,&#39;nf&#39;,&#39;na&#39;,&#39;za&#39;,&#39;aq&#39;,&#39;gs&#39;,&#39;eu&#39;,&#39;pw&#39;,&#39;pn&#39;,&#39;pt&#39;,&#39;jp&#39;,&#39;se&#39;,&#39;ch&#39;,&#39;sv&#39;,&#39;ws&#39;,&#39;yu&#39;,&#39;sl&#39;,&#39;sn&#39;,&#39;cy&#39;,&#39;sc&#39;,&#39;sa&#39;,&#39;cx&#39;,&#39;st&#39;,&#39;sh&#39;,&#39;kn&#39;,&#39;lc&#39;,&#39;sm&#39;,&#39;pm&#39;,&#39;vc&#39;,&#39;lk&#39;,&#39;sk&#39;,&#39;si&#39;,&#39;sj&#39;,&#39;sz&#39;,&#39;sd&#39;,&#39;sr&#39;,&#39;sb&#39;,&#39;so&#39;,&#39;tj&#39;,&#39;tw&#39;,&#39;th&#39;,&#39;tz&#39;,&#39;to&#39;,&#39;tc&#39;,&#39;tt&#39;,&#39;tn&#39;,&#39;tv&#39;,&#39;tr&#39;,&#39;tm&#39;,&#39;tk&#39;,&#39;wf&#39;,&#39;vu&#39;,&#39;gt&#39;,&#39;ve&#39;,&#39;bn&#39;,&#39;ug&#39;,&#39;ua&#39;,&#39;uy&#39;,&#39;uz&#39;,&#39;es&#39;,&#39;eh&#39;,&#39;gr&#39;,&#39;hk&#39;,&#39;sg&#39;,&#39;nc&#39;,&#39;nz&#39;,&#39;hu&#39;,&#39;sy&#39;,&#39;jm&#39;,&#39;am&#39;,&#39;ac&#39;,&#39;ye&#39;,&#39;iq&#39;,&#39;ir&#39;,&#39;il&#39;,&#39;it&#39;,&#39;in&#39;,&#39;id&#39;,&#39;uk&#39;,&#39;vg&#39;,&#39;io&#39;,&#39;jo&#39;,&#39;vn&#39;,&#39;zm&#39;,&#39;je&#39;,&#39;td&#39;,&#39;gi&#39;,&#39;cl&#39;,&#39;cf&#39;,&#39;cn&#39;,&#39;yr&#39;,&#39;com&#39;,&#39;arpa&#39;,&#39;edu&#39;,&#39;gov&#39;,&#39;int&#39;,&#39;mil&#39;,&#39;net&#39;,&#39;org&#39;,&#39;biz&#39;,&#39;info&#39;,&#39;pro&#39;,&#39;name&#39;,&#39;museum&#39;,&#39;coop&#39;,&#39;aero&#39;,&#39;xxx&#39;,&#39;idv&#39;,&#39;me&#39;,&#39;mobi&#39;,&#39;asia&#39;,&#39;ax&#39;,&#39;bl&#39;,&#39;bq&#39;,&#39;cat&#39;,&#39;cw&#39;,&#39;gb&#39;,&#39;jobs&#39;,&#39;mf&#39;,&#39;rs&#39;,&#39;su&#39;,&#39;sx&#39;,&#39;tel&#39;,&#39;travel&#39;
  );
   
  if(!preg_match("/^http/is", $url)){
    $url="http://".$url;
  }
 
  $res = null;
  $res->domain = null;
  $res->host = null;
  $url_parse = parse_url(strtolower($url));
  $urlarr = explode(".", $url_parse[&#39;host&#39;]);
  $count = count($urlarr);
   
  if($count <= 2){
    #当域名直接根形式不存在host部分直接输出
    $res->domain = $url_parse[&#39;host&#39;];
  }elseif($count > 2){
    $last = array_pop($urlarr);
    $last_1 = array_pop($urlarr);
    $last_2 = array_pop($urlarr);
     
    $res->domain = $last_1.&#39;.&#39;.$last;
    $res->host = $last_2;
     
    if(in_array($last, $state_domain)){
      $res->domain=$last_1.&#39;.&#39;.$last;
      $res->host=implode(&#39;.&#39;, $urlarr);
    }
     
    if(in_array($last_1, $state_domain)){
      $res->domain = $last_2.&#39;.&#39;.$last_1.&#39;.&#39;.$last;
      $res->host = implode(&#39;.&#39;, $urlarr);
    }
    #print_r(get_defined_vars());die;
  }
  return $res;
   
}
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to programmatically implement PHP to append content to a txt file

About PHP Usage of template engine Smarty’s built-in functions section and sectionelse

The above is the detailed content of PHP implements extracting root domain name through URL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!