Jump to content

expression not working 100%


Destramic

Recommended Posts

im trying to get a match on the pattern that will look like  :game, :league, :whatever ...etc...

but the script below will only find 1 match when there should be 2...can anyone please help?

 

<?php

$pattern = "game/:game/league/:league";

		if (preg_match_all('/(?<=\w+/', $pattern, $matches))
		{
			foreach ($matches as $match)
			{	
				echo $match_count = count($matches);
                                }
                        }
?>

Link to comment
https://forums.phpfreaks.com/topic/244808-expression-not-working-100/
Share on other sites

Actually that matches two, you are just mis-interpreting the returned data. If you replace the foreach loop with a print_r or var_dump, you will see that $matches is actually an array that contains an array with two matches in it. The way preg_match_all works is to return an array that contains an array of all the full matches (which is $matches[0]) plus an array for every capture group in the pattern. If you actually want to see what you matched it would be...

 

foreach ($matches[0] as $match) {
    echo $match;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.