Bug in preg_match() ?

php.internals

Michael Spector

22 years ago
Hi, This script: <? $line = "Use operator m// for matching strings"; $pattern = "m//"; if(preg_match("/$pattern/", $line, $match)) { print $match[0]."\n"; } ?> outputs the following error: <br /> <b>Warning</b>: Unknown modifier '/' in <b>/home/michael/bug.php</b> on line <b>5</b><br /> I thought it should find the match "m//" ... Can you say, why search of the ending delimiter in a pattern cannot be performed in a simple way, as you can see in the attached patch ? Thanks.

Nuno Lopes

22 years ago
> Hi, > > This script: > > <? > $line = "Use operator m// for matching strings"; > $pattern = "m//"; > > if(preg_match("/$pattern/", $line, $match)) { > print $match[0]."\n"; > } > ?>
There is no bug here! The final pattern is "/m///". When using // as delimeters, you must escape them, so you would need to set $pattern="m\/\/" *or* using other delimeter such as '~': preg_match("~$pattern~",...) Nuno

Curt Zirzow

22 years ago
* Thus wrote Michael Spector:
> > Hi, > > This script: > > <? > $line = "Use operator m// for matching strings"; > $pattern = "m//"; > > if(preg_match("/$pattern/", $line, $match)) { > print $match[0]."\n"; > } > ?> > > outputs the following error: > > <br /> > <b>Warning</b>: Unknown modifier '/' in <b>/home/michael/bug.php</b> on > line <b>5</b><br /> > > > I thought it should find the match "m//" ... > > Can you say, why search of the ending delimiter in a pattern cannot > be performed in a simple way, as you can see in the attached patch ?
Because of side effects like this: $pattern = "Foo//i"; if(preg_match("/$pattern", $line, $match)) { With your patch the preg_match has silently turned into a caseINsensitve search. Just use preg_quote($pattern, '/'); Curt
-- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid!