Regular Expression Help

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?

$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>';
			


		}
		
	
	}

The results should contains only:

example.com
a.example.com
Thanks for your time!

By the way, I know that I can use the parse_url function but I am looking to do it with a Regex expression :slight_smile: