#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    496
    Rep Power
    88

    Extract domain from a hostname


    Hi,

    I'd like to extract the domain from a given hostname. For example:

    hostname: this.is.a.hostname.domain.com
    result: domain.com

    hostname: domain.com
    result: domain.com

    My current regexp (php) is:

    /([a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]{2,3})$/

    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 03:47 AM.
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2001
    Location
    England
    Posts
    967
    Rep Power
    13
    I'm a little rusty with my regex and there might be something simpler you could use, but try:

    /(([a-z0-9]+)(-[a-z0-9]+)?)(\.[a-z]{2}\.[a-z]{2}|\.[a-z]{2,4})$/i

    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:

    domain.name
    domain.com
    domain.ch
    domain.co.uk
    domain-name.com
    my-domain-name.com
    123-reg.co.uk
    www.123-reg.co.uk
    ab.com
    my.really.obscure.domain.com
    http://www.google.com
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,689
    Rep Power
    6351
    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.

    -Dan
    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.

IMN logo majestic logo threadwatch logo seochat tools logo