Re: preg_match too greedy
| From: | Jim Lucas | Date: | Wed, 29 Jul 2009 18:07:00 +0000 |
| Subject: | Re: preg_match too greedy | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-296011@lists.php.net to get a copy of this message | ||
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