Forum Moderators: coopster & phranque

Message Too Old, No Replies

Parse out proper domain

         

Regent

10:56 pm on Mar 29, 2005 (gmt 0)

10+ Year Member



I need some perl code to parse out the proper domain of a given url. By proper domain, I mean the host domain minus any subdomains. For example:

URL: [subdomain.domain.com...]

Proper domain: domain.com

Thanks for any help.

timster

1:49 pm on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about this:

m#^(?:\w+\://)?(?:[\w\_\-]+\.)*([\w\_\-]+\.[a-z]{2,3})\b#i and $match = $1;

(?:\w+\://)? - Matches & sets aside "http://" "https://" "ftp://" etc., if found

(?:[\w\_\-])* - Matches & sets aside 1 or more subdomains, if found

([\w\_\-]+\.[a-z]{2,3}) - Matches domain - n characters followed by a dot, followed by 2 or 3 characters (i.e., a top level domain)

This should work unless you are matching domains with strange characters in them. "Strange" here means anything besides a letter, number, hyphen, or underscore.