Page 1 of 1

extract domains in string

Posted: Sat Oct 09, 2010 8:40 pm
by yacahuma
I have a string that look like this
domain1.netdomain2.orgdomain3.infodomain4.com

I want to get a nice array like
domain1.net
domain2.org
domain3.info
domain4.com

So far this is what I got

Code: Select all

 $domains = preg_split("/\.org|\.com|\.net|\.info/", strtolower($string));
the problem I just get domains with no extension.
domain1
domain2
domain3
domain4


My regex power are pathetic.

Re: extract domains in string

Posted: Sun Oct 10, 2010 1:36 pm
by John Cartwright
You might have a bit of fun supporting subdomains, atleast without using some kind of delimeter (which is beyond me why your forced into parsing such a unflexible format).

Code: Select all

preg_match_all("~[a-z0-9]+\.(org|com|net|info)~i", strtolower($string), $matches);

Re: extract domains in string

Posted: Sun Oct 10, 2010 5:09 pm
by yacahuma
works great. thank you. I was doing some page scraping. Thats what I got from xpath.