-3

Here's are some example hostnames:

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

The domain is always example.com and doesn't change.

How do I get the subdomain directly next to example.com and then get everything before that?

For example, I'd like to be able to do something along these lines:

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

And get this (depending on which of the examples above is used):

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

I'm trying to do this in PHP and I've tried a few options, but none of them seem to be working :/

xowim46108
  • 97
  • 1
  • 4

1 Answers1

1

Take a look at the explode function. It will give you an array of values that are each word in the FQDN.

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

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

print_r($name_array);

The above would yield:

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

Then you can use the array to use or manipulate the values further.

Shawn

Shawn Taylor
  • 270
  • 2
  • 4
  • 11