# Bug in preg_match() ?

- Thread: https://externals.io/message/11092
- Original archive: https://news-web.php.net/php.internals/11092
- Started by Michael Spector on 2004-07-11T17:41:45+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #11092 — Michael Spector — 2004-07-11T17:41:45+00:00

URL: https://externals.io/message/11092#11092

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.

---

## #11097 — Nuno Lopes — 2004-07-11T19:07:42+00:00

URL: https://externals.io/message/11092#11097 · In reply to: #11092

> 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

---

## #11098 — Curt Zirzow — 2004-07-11T19:22:37+00:00

URL: https://externals.io/message/11092#11098 · In reply to: #11092

* 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!
