I need to get a domain and tld from a string.
I've tried:
parse_url('https://www.google.com', PHP_URL_HOST);
But www
still remains.
Is there a way to do this without knowing all of the tlds that are in existence?
subdomain.example.com
https://example.com
http://www.example.com
example.com
www.example.com
www.example.co.uk
All of the above should produce example.com
or example.co.uk
Note, im not trying to get the TLD only, I want the domain and TLD together.
CodePudding user response:
Is there a way to do this without knowing all of the tlds that are in existence?
Unfortunately, no.
As far as the domain name system is concerned, the TLD of all of the following is is ".uk":
- www.example.co.uk
- example.co.uk
- nhs.uk
- login.mydoctor.nhs.uk
- a.w.e.s.o.m.e.uk
The only way to look at "www.example.co.uk" and decide you want the output "example.co.uk" is to apply one of two pieces of knowledge:
- Common prefixes, such as "www.", which you want to strip off
- Common suffixes, such as ".co.uk", which you want to treat as "almost TLD"
For the latter, there is a database maintained by Mozilla of "public suffixes". It is used for instance for determining the scope of cookies.
CodePudding user response:
$url = 'https://google.com';
$parse = parse_url($url);
echo $parse['host'];
The above code outputs the host of the domain without the leading protocol.