Regular Expression Help
Posted: Fri Jan 21, 2011 4:32 pm
Hi there,
I am trying to extract the ROOT DOMAIN from a list of URL.
I am having trouble with the regular expression to achieve this.
Here is my code so far, could somebody please correct my regular expression.
The results should contains only:
example.com
a.example.com
Thanks for your time!
I am trying to extract the ROOT DOMAIN from a list of URL.
I am having trouble with the regular expression to achieve this.
Here is my code so far, could somebody please correct my regular expression.
Code: Select all
$array = array('http://www.example.com/',
'http://www.example.com/a/a.com',
'http://www.example.com/a.com',
'http://example.com/',
'http://example.com/a/a.com',
'http://example.com/a.com',
'https://www.example.com/',
'https://www.example.com/a/a.com',
'https://www.example.com/a.com',
'https://example.com/',
'https://example.com/a/a.com',
'https://example.com/a.com',
'http://www.a.example.com/',
'http://www.a.example.com/a/a.com',
'http://www.a.example.com/a.com',
'http://a.example.com/',
'http://a.example.com/a/a.com',
'http://a.example.com/a.com',
'https://www.a.example.com/',
'https://www.a.example.com/a/a.com',
'https://www.a.example.com/a.com',
'https://a.example.com/',
'https://a.example.com/a/a.com',
'https://a.example.com/a.com');
//Check links from Same Root Url
for($x = 0; $x < count($array); $x++){
//Extract Root Domain
if (preg_match('/[http|https]+:\/\/[|www\.]*(.*)\//', $array[$x], $root_url)){
echo $root_url[1].'<br>';
}
}example.com
a.example.com
Thanks for your time!