从URL中获取域名

8

好的,在你说“哦,这很简单”的之前,我必须告诉你,我已经尝试了许多不同的方法来实现这个特定的事情,很长时间以来,我没有找到任何一个真正适用于任何URL任何域名的方法。

举例:

  • http://www.this-is-a-url.com = this-is-a-url.com
  • www.this-is-another.url.com/some-folder = this-is-another-url.com
  • subdomain.somesub.domain.com/index.php = domain.com
  • diff.erentltd.in = erentltd.in
  • www.andanotherone.org.uk = andanotherone.org.uk

那么,有什么想法吗?你知道任何有效的函数/脚本吗?


对于任何感兴趣的人:请看一下@bystwn22的答案。这是你可能能找到的最顺畅的解决方案之一!:-)


5
没有列出所有有效的国家和地区顶级域名清单,将无法正常工作。例如,something.com.tk 是有效的,但 something.com.at 将是无效的,因为 com.at 不是一个国家和地区顶级域名,而是一个普通域名。 - Rufinus
@Rufinus 我知道这个问题有多棘手... :S - Dr.Kameleon
你的第二个例子正确吗?我认为应该是www.this-is-another-url.com/some-folder = this-is-another-url.com - msturdy
这里有两个有用的链接:https://wiki.mozilla.org/TLD_List 和 http://data.iana.org/TLD/tlds-alpha-by-domain.txt - user1646111
1
我认为在不提前知道哪些子域名是重要的情况下,无法区分 importantsubdomain.domain.comunimportantsubdomain.domain.com。很容易排除 www.,但几乎任何其他内容都可能很重要。甚至不能依赖于有效 TLD 列表以及哪些常见的 TLD 具有三级域名,因为域名行业即将出现许多非常大的变化。 - Spudley
3个回答

3
好的,请试一下这个方法,我知道这个问题很棘手。
<?php
  $urls = array(
    "http://www.this-is-a-url.com",
    "www.this-is-another-url.com/some-folder",
    "subdomain.somesub.domain.com/index.php",
    "diff.erentltd.in",
    "www.andanotherone.org.uk"
  );

  foreach( $urls as $url ) {
    var_dump( get_domain( $url ) );
  }

  /** Output **/
  // string(17) "this-is-a-url.com"
  // string(23) "this-is-another-url.com"
  // string(10) "domain.com"
  // string(11) "erentltd.in"
  // string(20) "andanotherone.org.uk"
?>

函数 get_domain

<?php
  function get_domain( $url ) {
    $regex  = "/^((http|ftp|https):\/\/)?([\w-]+(\.[\w-]+)+)([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?$/i";
    if ( !preg_match( $regex, $url, $matches ) ) {
      return false;
    }
    $url    = $matches[3];
    $tlds   = array( 'ac', 'ad', 'ae', 'aero', 'af', 'ag', 'ai', 'al', 'am', 'an', 'ao', 'aq', 'ar', 'arpa', 'as', 'asia', 'at', 'au', 'aw', 'ax', 'az', 'ba', 'bb', 'bd', 'be', 'bf', 'bg', 'bh', 'bi', 'biz', 'bj', 'bm', 'bn', 'bo', 'br', 'bs', 'bt', 'bv', 'bw', 'by', 'bz', 'ca', 'cat', 'cc', 'cd', 'cf', 'cg', 'ch', 'ci', 'ck', 'cl', 'cm', 'cn', 'co', 'com', 'coop', 'cr', 'cu', 'cv', 'cx', 'cy', 'cz', 'de', 'dj', 'dk', 'dm', 'do', 'dz', 'ec', 'edu', 'ee', 'eg', 'er', 'es', 'et', 'eu', 'fi', 'fj', 'fk', 'fm', 'fo', 'fr', 'ga', 'gb', 'gd', 'ge', 'gf', 'gg', 'gh', 'gi', 'gl', 'gm', 'gn', 'gov', 'gp', 'gq', 'gr', 'gs', 'gt', 'gu', 'gw', 'gy', 'hk', 'hm', 'hn', 'hr', 'ht', 'hu', 'id', 'ie', 'il', 'im', 'in', 'info', 'int', 'io', 'iq', 'ir', 'is', 'it', 'je', 'jm', 'jo', 'jobs', 'jp', 'ke', 'kg', 'kh', 'ki', 'km', 'kn', 'kp', 'kr', 'kw', 'ky', 'kz', 'la', 'lb', 'lc', 'li', 'lk', 'lr', 'ls', 'lt', 'lu', 'lv', 'ly', 'ma', 'mc', 'md', 'me', 'mg', 'mh', 'mil', 'mk', 'ml', 'mm', 'mn', 'mo', 'mobi', 'mp', 'mq', 'mr', 'ms', 'mt', 'mu', 'museum', 'mv', 'mw', 'mx', 'my', 'mz', 'na', 'name', 'nc', 'ne', 'net', 'nf', 'ng', 'ni', 'nl', 'no', 'np', 'nr', 'nu', 'nz', 'om', 'org', 'pa', 'pe', 'pf', 'pg', 'ph', 'pk', 'pl', 'pm', 'pn', 'pr', 'pro', 'ps', 'pt', 'pw', 'py', 'qa', 're', 'ro', 'rs', 'ru', 'rw', 'sa', 'sb', 'sc', 'sd', 'se', 'sg', 'sh', 'si', 'sj', 'sk', 'sl', 'sm', 'sn', 'so', 'sr', 'st', 'su', 'sv', 'sy', 'sz', 'tc', 'td', 'tel', 'tf', 'tg', 'th', 'tj', 'tk', 'tl', 'tm', 'tn', 'to', 'tp', 'tr', 'travel', 'tt', 'tv', 'tw', 'tz', 'ua', 'ug', 'uk', 'us', 'uy', 'uz', 'va', 'vc', 've', 'vg', 'vi', 'vn', 'vu', 'wf', 'ws', 'ye', 'yt', 'yu', 'za', 'zm', 'zw' );
    $parts  = array_reverse( explode( ".", $url ) );
    $domain = array();

    foreach( $parts as $part ) {
      $domain[] = $part;
      if ( !in_array( strtolower( $part ), $tlds ) ) {
        return implode( ".", array_reverse( $domain ) );
      }
    }
  }
?>

这里有两个有用的链接:https://wiki.mozilla.org/TLD_List 和 http://data.iana.org/TLD/tlds-alpha-by-domain.txt - user1646111
谢谢,我认为data.iana.org/TLD/tlds-alpha-by-domain.txt中的所有顶级域名已经在我的列表中了 :) - bystwn22
伙计,我认为你做到了。 (我说“认为”,因为我仍然无法相信它正在工作...哈哈 - 即使我已经彻底测试过了)。干得好!非常感谢! :-) - Dr.Kameleon

1

我致力于提供更为简单的解决方案。由于我们在使用parse_url时遇到了问题。

check("www.google.com");

function check($url) {
        if (!preg_match("/^http/", $url)) $url = "http://" . $url;
        echo preg_replace("/.*\.([^\.]+\.[^\.]+)/", "$1", parse_url ( $url, PHP_URL_HOST ));
}

1
不幸的是,这个输出结果是:this-is-a-url.comurl.comdomain.comerentltd.inorg.uk - Amal Murali
1
是的,我知道它不能处理英国的域名,你的也一样,因为你的逻辑是硬编码的。我们需要使用有效的顶级域名字典来使其真正起作用。 - DevZer0
@AmalMurali 不对。他的意思是你要检查.uk,但除了.uk之外还有更多的二级域名。 - Rufinus

1

你实际上需要两个列表:二级域名和顶级域名。

  1. 使用preg_matchparse_url从你的url中获取主机,假设它是subdomain.domain.org.uk

  2. 通过点将其分解为数组,并取该数组的最后两个元素,再次连接成点(org.uk)。如果这是二级域名之一,则添加数组的前一个元素,即可得到你的域名(domain.org.uk)。

  3. 否则,你的域名就是你在步骤2中检查过的内容(如果数组的最后一个元素是顶级域名之一,则可以跳过此检查,如果你非常确定该域名有效)。如果你原始的主机是subdomain.domain.com,那么你已经检查了domain.com不是二级域名,这意味着domain.com就是你要找的东西。

这里是二级域名列表。或者你可以尝试找到更好的。


我已经查看了列表,实际上您需要连接最后3个元素(如果它被分解为3个或更多元素),并执行与最后2个元素相同的检查,因为二级域名可能像“ecape.school.za”一样。但是,这并不会改变算法太多。 - mr. Pavlikov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接