September 11th, 2009, 04:40 AM
Extract domain from a hostname
I'd like to extract the domain from a given hostname. For example:
My current regexp (php) is:
I'm very bad at regexp so i need some help. This current example works for domains that end in com, ru or something that contains 2 or 3 characters. But how do i specify that the ending of the domain can be:
either: 2 or 3 characters long
or: 2 or 3 characters long, followed by a dot, followed by to or 3 more characters (like co.uk)
How should i modify this expression?
Also there's a problem of knowing if the co.uk part is actually a domain ending or is it adomain itself (like ab.com).
Last edited by murklys; September 11th, 2009 at 04:47 AM.
September 13th, 2009, 05:26 PM
I'm a little rusty with my regex and there might be something simpler you could use, but try:
Also, AFAIK, domain names don't typically include underscores. You're out of luck if you're trying to match ip addresses with this expression, or if the user ends the domain with / or whatever. I don't really know where you're getting your data from, though, so it's difficult to say whether or not this will be a problem for you.
I tried this against the following (some of which are random domains, some real), and the expression worked out against all of them:
September 14th, 2009, 11:57 AM
You could just use [PHPNET="parse_url"]parse_url[/PHPNET]. Remember, in PHP, there's almost always a built-in function for what you're trying to do.
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question
or you're a Help Vampire.
Trying to argue intelligently? Please read this.