View Single Post
Old 2006-10-18, 02:43 AM   #12
QuickDraw
Heh Heh Heh! Lisa! Vampires are make believe, just like elves and gremlins and eskimos!
 
Join Date: Jan 2006
Posts: 72
Here's a function I use to get only the domain from a URL.. I think that's what you're after? Just pass it a url and it will return the domain.

Code:
function getdomain($url)
{
    // patterns we need to match
    $pattern_hostname = '/^(http:\/\/)?([^\/]+)/i';
    $pattern_domain = '/[^\.\/]+\.[^\.\/]+(\.[^\.\/]{2})?$/';
    // extract hostname from URL string
    @preg_match($pattern_hostname, $url, $matches);
     $hostname = $matches[2];
    // extract sld.tld(.cctld if exists)
    @preg_match($pattern_domain, $hostname, $matches);
    $domain = $matches[0];
    return $domain;
}
The only thing is.. it can't differentiate between a domain and an IP - if you pass it an IP, it gives you the last 2 octets as the domain. It would be easy enough to validate that before or after you use the function though.

Hope this helps..
QD

Last edited by QuickDraw; 2006-10-18 at 02:55 AM..
QuickDraw is offline   Reply With Quote