如何获取主机名的不同部分



以下是一些主机名示例:

test.whatever.es.example.com
more.test.pages.fr.example.com
test.website.de.example.com

域始终为example.com,并且不会更改。

如何直接获取example.com旁边的子域,然后获取之前的所有内容?

例如,我希望能够沿着以下路线做一些事情:

echo "$domain - $sub_domain - $sub_sub_domains";

得到这个(取决于上面使用的例子(:

example.com - es - test.whatever
example.com - fr - more.test.pages
example.com - de - test.website

我正在尝试用PHP实现这一点,我尝试了一些选项,但似乎都不起作用:/

看看爆炸函数。它将为您提供一个值数组,这些值是FQDN中的每个单词。

https://www.php.net/manual/en/function.explode.php

$name_array = explode('.', 'test.whatever.es.example.com');
print_r($name_array);

以上将产生:

Array
(
    [0] => test
    [1] => whatever
    [2] => es
    [3] => example
    [4] => com
)

然后,您可以使用数组来进一步使用或操作这些值。

Shawn

最新更新