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
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)
我会做类似以下的事情:
// 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)
如果您想要一个快速简单的解决方案,无需外部调用并检查预定义的数组.与最受欢迎的答案不同,适用于"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)
我最终使用了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 );