PHP从子域获取域名

zuk*_*uk1 26 php subdomain dns

我需要编写一个函数来解析包含域名的变量.我最好用一个例子来解释这个,变量可以包含以下任何一个:

here.example.com
example.com
example.org
here.example.org
Run Code Online (Sandbox Code Playgroud)

但是当通过我的函数时,所有这些必须返回example.com或example.co.uk,基本上是域名.我确定我以前做过这个,但我一直在谷歌搜索大约20分钟,但找不到任何东西.任何帮助,将不胜感激.

编辑:忽略.co.uk,假设通过此功能的所有域都有3个字母的TLD.

Sam*_*son 38

Stackoverflow问题档案:


print get_domain("http://somedomain.co.uk"); // outputs 'somedomain.co.uk'

function get_domain($url)
{
  $pieces = parse_url($url);
  $domain = isset($pieces['host']) ? $pieces['host'] : '';
  if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
    return $regs['domain'];
  }
  return false;
}
Run Code Online (Sandbox Code Playgroud)

  • 当tld为2个字符长时,这不适用于3个字符长的域.www.exg.ie将www.exg.ie作为域名返回.有任何想法吗? (4认同)

Gum*_*mbo 7

我会做类似以下的事情:

// hierarchical array of top level domains
$tlds = array(
    'com' => true,
    'uk' => array(
        'co' => true,
        // …
    ),
    // …
);
$domain = 'here.example.co.uk';
// split domain
$parts = explode('.', $domain);
$tmp = $tlds;
// travers the tree in reverse order, from right to left
foreach (array_reverse($parts) as $key => $part) {
    if (isset($tmp[$part])) {
        $tmp = $tmp[$part];
    } else {
        break;
    }
}
// build the result
var_dump(implode('.', array_slice($parts, - $key - 1)));
Run Code Online (Sandbox Code Playgroud)

  • @user198729 - http://publicsuffix.org/list/ 已取代@Gumbo 链接到的列表。据我统计 (`cat effective_tld_names.dat | grep -v "^//" | grep -v "^$" | wc -l`) 目前有 3692 个条目,所以还不错。 (2认同)

sun*_*100 7

如果您想要一个快速简单的解决方案,无需外部调用并检查预定义的数组.与最受欢迎的答案不同,适用于"www.domain.gallery"等新域名.

function get_domain($host){
  $myhost = strtolower(trim($host));
  $count = substr_count($myhost, '.');
  if($count === 2){
    if(strlen(explode('.', $myhost)[1]) > 3) $myhost = explode('.', $myhost, 2)[1];
  } else if($count > 2){
    $myhost = get_domain(explode('.', $myhost, 2)[1]);
  }
  return $myhost;
}
Run Code Online (Sandbox Code Playgroud)
  • domain.com - > domain.com
  • sub.domain.com - > domain.com
  • www.domain.com - > domain.com
  • www.sub.sub.domain.com - > domain.com
  • domain.co.uk - > domain.co.uk
  • sub.domain.co.uk - > domain.co.uk
  • www.domain.co.uk - > domain.co.uk
  • www.sub.sub.domain.co.uk - > domain.co.uk
  • domain.photography - > domain.photography
  • www.domain.photography - > domain.photography
  • www.sub.domain.photography - > domain.photography


Nik*_*ski 6

我最终使用了Mozilla的数据库.

这是我的代码:

fetch_mozilla_tlds.php包含缓存algorhythm.这条线很重要:

$mozillaTlds = file('http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1');
Run Code Online (Sandbox Code Playgroud)

应用程序内部使用的主文件是:

function isTopLevelDomain($domain)
{
    $domainParts = explode('.', $domain);
    if (count($domainParts) == 1) {
        return false;
    }

    $previousDomainParts = $domainParts;
    array_shift($previousDomainParts);

    $tld = implode('.', $previousDomainParts);

    return isDomainExtension($tld);
}

function isDomainExtension($domain)
{
    $tlds = getTLDs();

    /**
     * direct hit
     */
    if (in_array($domain, $tlds)) {
        return true;
    }

    if (in_array('!'. $domain, $tlds)) {
        return false;
    }

    $domainParts = explode('.', $domain);

    if (count($domainParts) == 1) {
        return false;
    }

    $previousDomainParts = $domainParts;

    array_shift($previousDomainParts);
    array_unshift($previousDomainParts, '*');

    $wildcardDomain = implode('.', $previousDomainParts);

    return in_array($wildcardDomain, $tlds);
}

function getTLDs()
{
    static $mozillaTlds = array();

    if (empty($mozillaTlds)) {
        require 'fetch_mozilla_tlds.php';
        /* @var $mozillaTlds array */
    }

    return $mozillaTlds;
}
Run Code Online (Sandbox Code Playgroud)

更新:
数据库已经发展,现在可以在自己的网站上找到 - http://publicsuffix.org/


小智 5

几乎可以肯定,你要找的是:

http://www.dkim-reputation.org/regdom-libs/

它是一个PHP库,利用在publicsuffix.org/list/收集的各种TLD的完整列表(尽可能实用),并将其包装在一个漂亮的小功能中.

一旦包含库,它就像以下一样简单:

$registeredDomain = getRegisteredDomain( $domain );

  • https://github.com/Synchro/regdom-php上有更新版本,但多年来一直没有更新.到目前为止,https://github.com/jeremykendall/php-domain-parser和https://github.com/layershifter/TLDExtract似乎是我发现执行此任务的最新库. (2认同)