Preg_Match help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
J0kerz
Forum Commoner
Posts: 37
Joined: Fri May 29, 2009 2:51 pm

Preg_Match help

Post 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!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Preg_Match help

Post 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.
User avatar
J0kerz
Forum Commoner
Posts: 37
Joined: Fri May 29, 2009 2:51 pm

Re: Preg_Match help

Post by J0kerz »

Thanks! It seems ti be working fine now

Code: Select all

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