How can i return just the TLD
of a domain name
我怎样才能返回域名的*域名
for example, if i had the following:
例如,如果我有以下内容:
www.domain.co.uk
i want to return just the .co.uk
www.domain.co.uk我想要返回.co.uk
and the same for domain.co.uk
和domain.co.uk一样
and the same for all other TLDs
(.com, .co, .org etc)
所有其他TLD(.com,.co,.org等)也是如此
Is there an easy way to do this in PHP without using Regex
有没有使用Regex在PHP中执行此操作的简单方法
I was thinking of using explode
but im not too familiar with that
我正在考虑使用爆炸但我不太熟悉它
4 个解决方案
#1
2
You can use a library called tldextract.php
.
您可以使用名为tldextract.php的库。
Find it here https://github.com/data-ac-uk/equipment/blob/master/_misc/tldextract.php
在这里找到它https://github.com/data-ac-uk/equipment/blob/master/_misc/tldextract.php
Usage is very simple:
用法很简单:
$extract = new TLDExtract();
$components = $extract('http://forums.bbc.co.uk/');
echo $components['tld']; // co.uk
Now how simple was that?
那现在有多简单?
If you read the code, you can see that it uses a large list of tlds in the fetchTldList()
function.
如果您阅读了代码,则可以看到它在fetchTldList()函数中使用了大量的tld。
#2
1
$returnHostName = parse_url($url, PHP_URL_HOST) //it will retrun 'domain' in your case
$returnArrayType = explode($returnHostName, $returnHostName); //explode it from string 'domain'
Okay now the variable returnArrayType contain your desired output but in the form of array you can get it from calling it's index.
好的,现在变量returnArrayType包含您想要的输出,但是以数组的形式,您可以通过调用它的索引来获取它。
check if It will work.
检查它是否有效。
#3
1
Not regex, not parse_url() will not help you.
不是正则表达式,而不是parse_url()对你没有帮助。
You need library that extacts TLD using Public Suffix List, I recomend to use TLDExtract.
你需要使用Public Suffix List来扩展TLD的库,我建议使用TLDExtract。
#4
0
This function work to www and not-www domain name. Not working to subdomains. For better results you need to use a regex or a library.
此功能适用于www和not-www域名。不适用于子域名。为了获得更好的结果,您需要使用正则表达式或库。
$domain = "www.domain.co.uk";
function tld($domain)
{
if (strstr('www', $domain)) {
$part = 3;
} else {
$part = 2;
}
$expld = explode('.', $domain);
if (count($expld) > $part) {
return $expld[count($expld) - 2] . '.' . $expld[count($expld) - 1];
} else {
return $expld[count($expld) - 1];
}
}
#1
2
You can use a library called tldextract.php
.
您可以使用名为tldextract.php的库。
Find it here https://github.com/data-ac-uk/equipment/blob/master/_misc/tldextract.php
在这里找到它https://github.com/data-ac-uk/equipment/blob/master/_misc/tldextract.php
Usage is very simple:
用法很简单:
$extract = new TLDExtract();
$components = $extract('http://forums.bbc.co.uk/');
echo $components['tld']; // co.uk
Now how simple was that?
那现在有多简单?
If you read the code, you can see that it uses a large list of tlds in the fetchTldList()
function.
如果您阅读了代码,则可以看到它在fetchTldList()函数中使用了大量的tld。
#2
1
$returnHostName = parse_url($url, PHP_URL_HOST) //it will retrun 'domain' in your case
$returnArrayType = explode($returnHostName, $returnHostName); //explode it from string 'domain'
Okay now the variable returnArrayType contain your desired output but in the form of array you can get it from calling it's index.
好的,现在变量returnArrayType包含您想要的输出,但是以数组的形式,您可以通过调用它的索引来获取它。
check if It will work.
检查它是否有效。
#3
1
Not regex, not parse_url() will not help you.
不是正则表达式,而不是parse_url()对你没有帮助。
You need library that extacts TLD using Public Suffix List, I recomend to use TLDExtract.
你需要使用Public Suffix List来扩展TLD的库,我建议使用TLDExtract。
#4
0
This function work to www and not-www domain name. Not working to subdomains. For better results you need to use a regex or a library.
此功能适用于www和not-www域名。不适用于子域名。为了获得更好的结果,您需要使用正则表达式或库。
$domain = "www.domain.co.uk";
function tld($domain)
{
if (strstr('www', $domain)) {
$part = 3;
} else {
$part = 2;
}
$expld = explode('.', $domain);
if (count($expld) > $part) {
return $expld[count($expld) - 2] . '.' . $expld[count($expld) - 1];
} else {
return $expld[count($expld) - 1];
}
}