【问题标题】:preg_match(): Compilation failed: missing )preg_match(): 编译失败: 缺失)
【发布时间】:2015-07-31 09:55:18
【问题描述】:

错误是:

PHP 警告:preg_match(): Compilation failed: missing ) 在 /home/siteraja/public_html/ar/wp-content/plugins/hide_my_wp/lib/mute-screamer/libraries/IDS/Monitor.php 中的偏移量 16第 312 行 这是第 227 行的代码:

  // check if this field is part of the exceptions
        if (is_array($this->exceptions)) {
            foreach($this->exceptions as $exception) {
                $matches = array();
                $reg = '/'.str_replace('%','[A-Za-z0-9_-]*', str_replace('.','\.',$exception)).'/i';
                if (preg_match($reg, $key))
                    return false;


            }
        }

【问题讨论】:

  • 发布您的原始字符串以及预期输出
  • 试试$reg = '/'.str_replace('%','[A-Za-z0-9_-]*', preg_quote($exception)).'/i';
  • 他的意思是发布 $this->exception$this->exceptions 的内容,以便我们可以看到传递到 $reg 计算中的内容
  • 您的异常字符串不会包含'/ 字符吗?
  • 我的回答有帮助吗?

标签: php regex wordpress


【解决方案1】:

您正在使用变量来初始化正则表达式模式。在不知道该字符串中可能包含哪些字符的情况下,您无法确定正则表达式语法是否正确。要消除这种风险,请在每个特殊字符前使用preg_quote,它将转义(=添加\)。所以,替换

$reg = '/'.str_replace('%','[A-Za-z0-9_-]*', str_replace('.','\.',$exception)).'/i';

$reg = '/'.str_replace('%','[A-Za-z0-9_-]*', preg_quote($exception)).'/i';

另外请注意,您只声明了$matches = array();,但您没有使用它,但这可能只是因为您没有显示整个代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多