从没有tld的url获取域名

时间:2022-08-23 10:37:48

I need to know how to get the domain name from a url without the tld.

我需要知道如何从没有tld的url获取域名。

This is what I have that works for .com, .info, etc but not for .co.uk

这就是我对.com,.info等有用的东西,但不适用于.co.uk

// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i',
"http://example.co.uk", $matches);
$host = $matches[1];

// get last two segments of host name
preg_match('/([^.]+)\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[1]}\n";

When I get it to call "example.co.uk" domain it just shows: "co" when I need it to show "example"

当我把它称为“example.co.uk”域时,它只显示:“co”当我需要它来显示“example”时

Thanks

1 个解决方案

#1


0  

Regex is falsy solution there, you need package that using Public Suffix List, only with it you can get correct result on complex TLDS.

正则表达式是那里的虚假解决方案,你需要使用Public Suffix List的包,只有它才能在复杂的TLDS上获得正确的结果。

I recomend TLDExtract for domain parsing, here is sample code that show diff:

我建议使用TLDExtract进行域解析,这里是显示diff的示例代码:

$extract = new LayerShifter\TLDExtract\Extract();

# For 'http://www.domain.com/site'

$result = $extract->parse('http://www.domain.com/site');
$result->getFullHost(); // will return 'www.domain.com'
$result->getRegistrableDomain(); // will return 'domain.com'
$result->getSuffix(); // will return 'com'

# For 'http://www.domain.co.uk/site'

$result = $extract->parse('http://www.domain.co.uk/site');
$result->getFullHost(); // will return 'www.domain.co.uk'
$result->getRegistrableDomain(); // will return 'domain.co.uk'
$result->getSuffix(); // will return 'co.uk'

#1


0  

Regex is falsy solution there, you need package that using Public Suffix List, only with it you can get correct result on complex TLDS.

正则表达式是那里的虚假解决方案,你需要使用Public Suffix List的包,只有它才能在复杂的TLDS上获得正确的结果。

I recomend TLDExtract for domain parsing, here is sample code that show diff:

我建议使用TLDExtract进行域解析,这里是显示diff的示例代码:

$extract = new LayerShifter\TLDExtract\Extract();

# For 'http://www.domain.com/site'

$result = $extract->parse('http://www.domain.com/site');
$result->getFullHost(); // will return 'www.domain.com'
$result->getRegistrableDomain(); // will return 'domain.com'
$result->getSuffix(); // will return 'com'

# For 'http://www.domain.co.uk/site'

$result = $extract->parse('http://www.domain.co.uk/site');
$result->getFullHost(); // will return 'www.domain.co.uk'
$result->getRegistrableDomain(); // will return 'domain.co.uk'
$result->getSuffix(); // will return 'co.uk'