How to parse domain name without subdomain part?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

How to parse domain name without subdomain part?

Post 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
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

may be network functions would help you. look @manual

you could use explode() but it's not a good idea..
MrSomeone
Forum Newbie
Posts: 7
Joined: Sun Oct 10, 2004 8:36 am

Post 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')
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post 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
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post 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
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

this is what i meant before with explode example...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You can find them yourself.. like I had to. :)
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

Post Reply