0

Is there any general way to get just the base domain (without subdomain)? For example, with the following URLs (assuming no fore-knowledge about what the domains or subdomains might be...)

www.example.com
mysub.example.com
example.com

www.example.co.uk
example.co.uk
xyz.example.co.uk

For the first three, I'd like to get just "example.com", for the last, "example.co.uk", with a function that could be used in any website/server/domain.

Is it possible?

ChrisNY
  • 3,917
  • 4
  • 27
  • 31

1 Answers1

-1

You could try

$arr = explode(".", $str);
echo $arr[count($arr)-2];

with additional error handling

But the thing with .co.uk and so on is very difficult and sometimes new tlds are released

I found this: Get root DNS entry from php server; get domain name without www, ect

You also should find something in the net

Community
  • 1
  • 1
Hendrik
  • 131
  • 1
  • 10