获取没有 TLD 的域名

发布于 2024-12-09 18:19:35 字数 429 浏览 0 评论 0原文

我这里有这段代码:

    // get host name from URL
    preg_match('@^(?:http://)?([^/]+)@i',
    "http://www.joomla.subdomain.php.net/index.html", $matches);
    $host = $matches[1];

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

输出将是 php.net

我只需要 php 而无需 .net

I have this code right here:

    // get host name from URL
    preg_match('@^(?:http://)?([^/]+)@i',
    "http://www.joomla.subdomain.php.net/index.html", $matches);
    $host = $matches[1];

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

The output will be php.net

I need just php without .net

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤独岁月 2024-12-16 18:19:35

虽然正则表达式在这里很好,但我建议 parse_url

$host = parse_url('http://www.joomla.subdomain.php.net/index.html', PHP_URL_HOST);
$domains = explode('.', $host);
echo $domains[count($domains)-2];

这适用于TLD 类似于 .com、.org、.net 等,但不适用于 .co.uk 或 .com.mx。您需要更多的逻辑(很可能是 tld 数组)来解析它们。

Although regexes are fine here, I'd recommend parse_url

$host = parse_url('http://www.joomla.subdomain.php.net/index.html', PHP_URL_HOST);
$domains = explode('.', $host);
echo $domains[count($domains)-2];

This will work for TLD's like .com, .org, .net, etc. but not for .co.uk or .com.mx. You'd need some more logic (most likely an array of tld's) to parse those out .

沐歌 2024-12-16 18:19:35

将第二个正则表达式的第一部分分组为 /([^.]+)\.[^.]+$/$matches[1] 将是 php

Group the first part of your 2nd regex into /([^.]+)\.[^.]+$/ and $matches[1] will be php

岁月如刀 2024-12-16 18:19:35

迟到的答案,它不适用于子域,但它确实适用于任何 tld (co.uk、com.de 等):

$domain = "somesite.co.uk";
$domain_solo = explode(".", $domain)[0];
print($domain_solo);

演示

Late answer and it doesn't work with subdomains, but it does work with any tld (co.uk, com.de, etc):

$domain = "somesite.co.uk";
$domain_solo = explode(".", $domain)[0];
print($domain_solo);

Demo

趁年轻赶紧闹 2024-12-16 18:19:35

这非常简单:

function get_tld($domain) {
    $domain=str_replace("http://","",$domain); //remove http://
    $domain=str_replace("www","",$domain); //remowe www
    $nd=explode(".",$domain);
    $domain_name=$nd[0];
    $tld=str_replace($domain_name.".","",$domain);
    return $tld;
}

要获取域名,只需返回 $domain_name,它仅适用于顶级域名。对于子域名,您将获得子域名。

It's really easy:

function get_tld($domain) {
    $domain=str_replace("http://","",$domain); //remove http://
    $domain=str_replace("www","",$domain); //remowe www
    $nd=explode(".",$domain);
    $domain_name=$nd[0];
    $tld=str_replace($domain_name.".","",$domain);
    return $tld;
}

To get the domain name, simply return $domain_name, it works only with top level domain. In the case of subdomains you will get the subdomain name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文