Re: preg_match too greedy

From: Date: Thu, 30 Jul 2009 02:06:07 +0000
Subject: Re: preg_match too greedy
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-296043@lists.php.net to get a copy of this message
On 07/29/2009 02:07 PM, Jim Lucas wrote:
b wrote:
I'm trying to figure out how to test if a string matches *exactly* another string, using a regexp pattern. The manual says that ereg() is deprecated (in favour of what?) and preg_match() is giving me trouble. The problem is that I'm passing the end-of-line delimiter ($) but it seems to be ignored. An example: -- snip -- header('Content-type: text/plain'); $url = '/foo(/)?'; $test = 'foo/bar'; $pattern = '%^'.$url.'$%U'; echo "${url} :: ${test}\n"; echo (preg_match($pattern, $test) != false)
     ? 'match'
     : 'no match';
-- snip -- I expected 'no match' but get 'match'. The reason for the (/)? is to allow for a trailing slash. I've added a '$' to specify that I want to test for the exact string. However, preg_match() tests for the existence of a string and appears to ignore the '$'. How do I do this?
cut/paste your code and it works for me. Jim Lucas
Works, meaning you get 'match', or 'no match'? It should be the latter, but I'm seeing the former. I'm using 5.2.9, btw.

« previous php.general (#296043) next »