Page 1 of 1

Preg_Match help

Posted: Thu Jun 10, 2010 11:15 am
by J0kerz
Hey there,

I have a code that sort out an array an make sure to keep only the URL that are not in their "Index" form/root domain form.

My regular expression is working but there is still a small thing that insnt working.

Code: Select all

<?php
	
	$array = array('http://www.example.com', 'http://example.com', 'http://this.example.com', 'http://www.example.com/page.html', 'http://example.com/page.html', 'http://this.example.com/page.html', 'http://www.example.com/?page=1', 'http://example.com/?page=1', 'http://this.example.com/?page=1');

	for ($x = 0; $x < count($array) - 1; $x++){
	

		$pregmatch = preg_match('-(?i)(?m:^)(\w+://.+/\w.*)(?:\v|\z)+-', $array[$x]);
		
		if ($pregmatch == 1)
		{
			echo $array[$x]."<br>";

		}
	

	}

?>

I would like the above code to output the following result at the end:
It doesnt seem to keep the URL followed by a "?". Can someone please correct the above regular expression :D

Thanks alot!

Re: Preg_Match help

Posted: Thu Jun 10, 2010 12:29 pm
by requinix
Because your expression says that immediately following some slash (after the domain) has to be a \w (letter, number, or underscore). The only slashes in those are followed by a ? so they don't match.

Re: Preg_Match help

Posted: Thu Jun 10, 2010 1:42 pm
by J0kerz
Thanks! It seems ti be working fine now

Code: Select all

$pregmatch = preg_match('-(?i)(?m:^)(\w+://.+/.+)(?:\v|\z)+-', $array[$x]);