Jump to content

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;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.