Stellan Coder
Stellan Coder

Reputation: 313

Get only domain name

i want only domain name without sub domain and path URL.

Example :

URL loading in browser : https://www.example.com/something. I want only example.com

I tried echo $_SERVER['HTTP_HOST']; and echo $_SERVER['SERVER_NAME']; but the output is www.example.com but it want example.com as output.

What to use?

Upvotes: -1

Views: 276

Answers (1)

thephper
thephper

Reputation: 2610

I would make a check for the existing of "www." in the beginning of the string, and if the string starts with "www." I would remove it.

Example:

$domain = substr($_SERVER['SERVER_NAME'],0,4) === 'www.' ? substr($_SERVER['SERVER_NAME'],4) : $_SERVER['SERVER_NAME'];
echo $domain;

Upvotes: 1

Related Questions