Get domain name (not subdomain)

ellescuba27

Member
Messages
273
Reaction score
3
Points
18
So, I'm looking for a PHP script that will help me output just the domain name, not the subdomain name (basically, just the part that's like "something.com", not "subdomain.something.com") . Any ideas?
E.g. on x10hosting if you are on "mysite.x10.mx" it outputs "x10.mx"
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
What do you want the result to be when the domain name has more than three components (e.g. "foo.bar.baz.bam.example.com")? Do you want a script that actually prints the domain, or a function that returns it?
 

ellescuba27

Member
Messages
273
Reaction score
3
Points
18
I want a function that returns it, and in the case of foo.bar.baz.bam.example.com, I only want example.com .
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Do you know anything about regular expressions?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Messages
17
Reaction score
0
Points
1
>"mysite.x10.mx" it outputs "x10.mx"

Strictly speaking the TLD (Top Level Domain), for that would be "mx", and there are all sorts of exceptions to achieve what you actually desire, as http://co.uk/ - is a legally formatted website, but there is no site situated for that address, but only at subdomains under that. However, an alternate example is that both http://co.cc/ and http://phreakie.co.cc/ are valid sites.

As an additional interesting point, the standard accepted prefix "www" is its self just a subdomain :)

As has been said, you could use RegEx to resolve this issue once you've worked out what you're actually seeking to achieve.

Personally the way I'd work this out would be to take a url i.e. https://example.com:80/ find the first occurance of :// (I suspect it should theoretically only legally be-able to occur once anyway)

Then with the remainder we have "example.com:80/"

If the string contains a colon :, then strip so you have all characters to the left of it resulting in example.com.

To simplify you want to find the first / after the initial :// portion, and then work backwards until you've read the required number of .'s

The more idea you have about the expected format, then the less alternatives you have to allow for.

i.e.

https://example.com
https://example.com:443
https://example.com:80/
ftp://example.com
http://www.example.com/forums/index.php

In essence as has already really been stated, it's essentially some very basic string parsing - as soon as you've decided exactly what you want the result to be for all the potential anomilities
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Strictly speaking the TLD (Top Level Domain), for that would be "mx",
True, but that's exactly why we've only been talking in terms of "domains" and "subdomains", not TLDs.

Personally the way I'd work this out would be to take a url i.e. https://example.com:80/ find the first occurance of :// (I suspect it should theoretically only legally be-able to occur once anyway)
Extracting a domain name from a URL is a different issue from extracting a domain from a subdomain. The former is easily handled by parse_url. There's no standard PHP function that explicitly does the latter.
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
The Top Level Domain is its self a domain, thus anything under the TLD strictly speaking is a sub-domain, isn't it?
Yes, but getting into TLDs is a diversion, as ellescuba27's requirements don't involve extracting the TLD separately from the site domain. The original comment about the TLD for "mysite.x10.mx" wasn't apropos. It doesn't matter what the TLDs for "mysite.x10.mx" or "foo.bar.com" or even "www.guardian.co.uk" are. All that matters is a rigorous distinction between "domain" and "subdomain".
 
Last edited:
Messages
17
Reaction score
0
Points
1
It doesn't matter what the TLDs for "mysite.x10.mx" or "foo.bar.com" or even "www.guardian.co.uk" are. All that matters is a rigorous distinction between "domain" and "subdomain".

I agree, all that matters is a rigorous distinction, and an explicit understanding of what is actually needed. Excluding the TLD, the next domain for www.guardian.co.uk is co.uk, but there are no sites on the co domain, for the TLD uk. There are however sites on the co domain for *.co.cc, however there is also a site at http://co.cc/

Ultimately I think the best generic solution to this problem would be to recursively get and attempt to resolve the possible domain, i.e. if co.uk resolves, return that, else recurse to guardian.co.uk.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
I agree, all that matters is a rigorous distinction, and an explicit understanding of what is actually needed. Excluding the TLD, the next domain for www.guardian.co.uk is co.uk, but there are no sites on the co domain, for the TLD uk. There are however sites on the co domain for *.co.cc, however there is also a site at http://co.cc/

Ultimately I think the best generic solution to this problem would be to recursively get and attempt to resolve the possible domain, i.e. if co.uk resolves, return that, else recurse to guardian.co.uk.

That's a good point. If the definition requires that the domain has a DNS record (whether of a specific type, or of any type), or must be a reachable server, a pattern-based approach won't be sufficient; gethostbyname/dns_get_record/checkdnsrr or ping (depending on the exact requirements) would be necessary, with an accompanying increase in time-cost. I suspect that ellescuba27's requirements don't go that far, but as we haven't heard anything in a month ...
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Well, the OP never came back.
Never cleared up whether he wanted a built in PHP function or a hand-rolled one.
Never clarified exactly what he wanted.
Guess the deadline for his homework passed.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Thanks, and FYI, I was never trying to start an intellectual pissing contest with you, I have no doubt that if I did I'd lose.

It's never such a foregone conclusion, and you didn't seem to be trying to start anything. I just wanted to keep the thread on-topic.
 

ellescuba27

Member
Messages
273
Reaction score
3
Points
18
LOL Yes, I am sorry I didn't post this thread and almost let it close! I am terrible at regex, is why I asked... is there any "proper" way to do it? I know there are Javascript functions to do so... "mysite.elementfx.com" returns "elementfx.com", "parttwoof.mysite.elementfx.com" returns "elementfx.com", "mysite.elementfx.co.uk" returns "elementfx.co.uk", just the main part like a magic TLD. I can find nothing.

However, I already solved this problem with another more creative solution that had to do with my case. So... problem solved. Which would be the reason I forgot... :(
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
However, I already solved this problem with another more creative solution that had to do with my case. So... problem solved. Which would be the reason I forgot... :(

In that case, would you mind posting your solution for the benefit others facing a similar issue? Just be warned that your solution may be picked apart and improved.
 

ellescuba27

Member
Messages
273
Reaction score
3
Points
18
Really it had to do with my case. I have two domains leading to the same place, theeducator.elementfx.com and theeducator.tk . I added a Custom Google Search and wanted it to search for results on the appropriate site (search results on elementfx when you're on there, otherwise not). So, to detect which one you're on, I wanted just to see the elementfx.com part, so I could tell if you're on there or not. In the end, I just ended up detecting the full name and adding a backup Google Search in case it was neither domain.
 
Top