PHP preg_match未知修饰符错误

时间:2016-05-07 08:58:50

标签: codeigniter

消息:preg_match():未知修饰符' p'

文件名:core / Router.php

行号:399

回溯:

文件:/home/spdcin/public_html/demo/no-waste/index.php 行:292 功能:require_once 我在第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;
        }
    }

2 个答案:

答案 0 :(得分:1)

您的正则表达式存在问题,PHP认为您尝试应用' p'修饰符,无效。

如果你这样做,你可能会知道你的正则表达式有什么问题:

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

您尝试对路由器进行编程这一事实表明$ key最有可能包含'#p' (在URL中很常见)。

解决方案:在你的情况下,你可以逃脱角色'#'用反斜杠。引自php文档: "如果需要在模式内匹配分隔符,则必须使用反斜杠对其进行转义。"

答案 1 :(得分:0)

如果我理解你的问题,请使用preg_quote()将$ key括起来:

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

此函数将自动转义$ key中的所有正则表达式命令。