Hello, I've spent hours trying to figure out how to get the domain name of an URL. For example: http://forums.digitalpoint.com/newthread.php?do=newthread ---> "digitalpoint.com" http://venturebeat.com/2008/01/10/thai-whiz-kid-launches-location-aware-twitter-like-service/ --> will return "venturebeat.com" if you know how to form Regular Expression to get the domain , please let me know. Thanks again.
I searched google and I found this. http://phosphorusandlime.blogspot.com/2007/08/php-get-base-domain.html Hope it helps...
$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.
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.
Erm, that only gets whatever is after the 2nd to last period. What about .co.uk, .com.sg, etc. domains?