>  기사  >  백엔드 개발  >  PHP는 URL을 통해 루트 도메인 이름 추출을 구현합니다.

PHP는 URL을 통해 루트 도메인 이름 추출을 구현합니다.

不言
不言원래의
2018-06-19 09:44:453757검색

이 글은 제가 프로젝트를 진행하면서 겪었던 일을 공유합니다. URL에서 루트 도메인 이름을 추출하려면 PHP를 사용해야 하고, 필요한 친구들은 이를 참고할 수 있습니다.

PHP는 루트 도메인을 추출합니다. 인터넷에 있는 코드 중엔 정확한 결과를 얻을 수 없는 코드가 많아서 직접 작성했습니다. 누구나 사용하고 버그를 신고할 수 있습니다.

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

위 내용은 다음과 같습니다. 이 기사의 전체 내용이 모든 사람에게 도움이 되기를 바랍니다. 더 많은 관련 내용을 보려면 PHP 중국어 웹사이트에 주목하세요.

관련 권장 사항:

PHP에서 txt 파일에 콘텐츠 추가를 프로그래밍 방식으로 구현하는 방법

PHP 템플릿 엔진 사용 정보 Smarty 내장 기능 섹션 및 섹션else

위 내용은 PHP는 URL을 통해 루트 도메인 이름 추출을 구현합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.