Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get only domain name with PHP

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading