0

Message: preg_match(): Unknown modifier 'p'

Filename: core/Router.php

Line Number: 399

Backtrace:

File: /home/spdcin/public_html/demo/no-waste/index.php Line: 292 Function: require_once iam getting this error on line 2

$key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);

        // Does the RegEx match?
         //line no 2
        if (preg_match('#^'.$key.'$#', $uri, $matches))
        {
            // Are we using callbacks to process back-references?
            if ( ! is_string($val) && is_callable($val))
            {
                // Remove the original string from the matches array.
                array_shift($matches);

                // Execute the callback using the values in matches as its parameters.
                $val = call_user_func_array($val, $matches);
            }
            // Are we using the default routing method for back-references?
            elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
            {
                $val = preg_replace('#^'.$key.'$#', $val, $uri);
            }

            $this->_set_request(explode('/', $val));
            return;
        }
    }
user_777
  • 845
  • 1
  • 9
  • 25
  • suppose `$key='#p';` – Jasen May 07 '16 at 09:04
  • Possible duplicate of [Warning: preg\_replace(): Unknown modifier '\]'](https://stackoverflow.com/questions/20705399/warning-preg-replace-unknown-modifier) – Toto Jul 04 '19 at 13:26

2 Answers2

1

There is a problem with your regex and PHP thinks you try to apply a 'p' modifier, which is not valid.

You will probably get to know what is wrong with your regex if you do :

echo '#^'.$key.'$#';

The fact that you try to program a router indicates that $key most probably contains '#p' (common in URLs).

Solution : In your case you can escape the character '#' with backslashes. Quoted from the php documentation : "If the delimiter needs to be matched inside the pattern it must be escaped using a backslash."

Gabriel Hautclocq
  • 3,230
  • 2
  • 26
  • 31
0

If I understand your problem correctly, surround $key with preg_quote() like this:

if (preg_match('#^'.preg_quote($key).'$#', $uri, $matches))

This function will automatically escape ALL regex commands in $key.

Platinum Fire
  • 502
  • 3
  • 10