preg_match too greedy
| From: | b | Date: | Wed, 29 Jul 2009 17:42:23 +0000 |
| Subject: | preg_match too greedy | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-296010@lists.php.net to get a copy of this message | ||
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?