>"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