Help: regular expression or trick to get the domain name of URL

Discussion in 'PHP' started by cbjunkie, Jul 8, 2008.

  1. #1
    cbjunkie, Jul 8, 2008 IP
  2. JLEville

    JLEville Peon

    Messages:
    147
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    JLEville, Jul 8, 2008 IP
  3. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Danltn, Jul 8, 2008 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $urlParts = parse_url($theURL);
    $domain = preg_match('#(?:^|\.)([a-z0-9]+\.(?:[a-z]{2,}|[a-z.]{5,6}))$#i', $urlParts['host'], $tmp) ? $tmp[1] : $urlParts['host'];
    PHP:
    That's taken from one of my projects where I need the other parts of the URL, hence using parse_url() to get the hostname. If you don't need anything else it's probably easiest to update the regex to work on the whole URL.

    The regex isn't perfect but seems to work on the more common domains.
     
    rodney88, Jul 8, 2008 IP
  5. Chaos King

    Chaos King Peon

    Messages:
    88
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Here is a handy function to get the domain name.


    function getDomainName ( $url )
    {
        $url = @parse_url ( $url );
        $domain = explode ( '.', $url['host'] );
        
        return $domain[ count($domain) - 2 ].'.'.end($domain);            
    }
    
    echo getDomainName ( 'http://forums.digitalpoint.com/newreply.php?do=postreply&t=923589' );
    // digitalpoint.com
    
    PHP:
    Its pretty basic and would return values for normal domain names. It will mess up if you use IP Addresses though, that's one thing that I caught now just writing it.
     
    Chaos King, Jul 8, 2008 IP
  6. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Erm, that only gets whatever is after the 2nd to last period. What about .co.uk, .com.sg, etc. domains?
     
    rodney88, Jul 9, 2008 IP
  7. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #7
    Exactly what I was thinking, stick with the Regex ;)
     
    Danltn, Jul 9, 2008 IP