Page 1 of 1
How to parse domain name without subdomain part?
Posted: Sun Oct 10, 2004 8:03 am
by tomfra
Ok, here is the problem...
I am getting the domain name through this code:
Code: Select all
$domain = ($_SERVER['HTTP_HOST']);
...it works but if there is subdomain or simply "www" in the HTTP_HOST e.g.
http://something.domain.com
then $domain will return "something.domain.com". I want it to return only "domain.com". I tried using strpos and substr to get rid of everything before the first dot but the subdomain part is not always present so that would be unreliable.
Any help is appreciated.
Thanks!
Tomas
Posted: Sun Oct 10, 2004 8:39 am
by mudkicker
may be network functions would help you. look @manual
you could use explode() but it's not a good idea..
Posted: Sun Oct 10, 2004 9:00 am
by MrSomeone
I would use
parse_url() and then check if the host returned has more than one . - and if it does, explode the string using ".", unset the first part of the array returned, and implode the remaining 2 parts ('domain' and 'com')
Posted: Sun Oct 10, 2004 9:01 am
by tomfra
Network functions? I am not sure I know what you are referring to exactly. Explode is relatively slow and the first element after http:// would be empty if no subdomain is given.
Tomas
Posted: Sun Oct 10, 2004 9:03 am
by mudkicker
Posted: Sun Oct 10, 2004 9:52 am
by tomfra
MrSomeone,
And how to solve problem with domains with two dots - e.g. domain.co.uk? I wanted to use only the last 2 array elements which would normally be domain & com but in this case it's not possible either.
Tomas
Posted: Sun Oct 10, 2004 9:59 am
by mudkicker
this is what i meant before with explode example...
Posted: Sun Oct 10, 2004 10:20 am
by feyd
here's how I've done this sort of thing in the past:
grabbed up the lists of TLDs throughout the world, compiled them into a list and created a pattern I can use in a regular expression. The domain is the first "word" to the left of the TLD.
This also could easily validate a domain.
Posted: Sun Oct 10, 2004 12:40 pm
by tomfra
And I thought this would be something easy
Feyd, do you have the TLD lists still or now where I can get an updated one?
Tomas
Posted: Sun Oct 10, 2004 12:50 pm
by feyd
You can find them yourself.. like I had to.

Posted: Sun Oct 10, 2004 2:39 pm
by mudkicker