【问题标题】:Regular expression is too large正则表达式太大
【发布时间】:2014-10-12 15:24:29
【问题描述】:

我遇到了这个功能的问题,

function _where($arg, $separator = ' '){
    if ($arg['where']){
        $operators = '( |=|!=|<>|<|<=|>|>=|=~|!~|\?|\?!)';

        foreach ($arg['where'] as $k => $v){
            if (preg_match('/\[(.*)\]/i', $v, $match)){
                foreach (explode('|', $match[1]) as $or){
                    $where[] = preg_replace('/(.*)'.$operators.'(.*)/i', '\\1\\2\'\\3\'', preg_replace('/\['.str_replace('|', '\|', $match[1]).'\]/i', $or, $v));
                }

                $result[] = '('.join(' or ', $where).')'."\r\n";
            } elseif ($v != 'and' and $v != 'or' and $v != 'xor'){
                $result[] = preg_replace('/(.*)'.$operators.'(.*)/i', '\\1\\2\'\\3\'', $v)."\r\n";
            } else {
                $result[] = $v."\r\n";
            }
        }

        return 'where '.str_replace(array('!~', '=~'), array('not like', 'like'), join($separator, $result));
    }
}

结果是,

PHP Warning:  preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Compilation failed: regular expression is too large at offset 63088

我正在使用相同的代码运行另一个站点,但那里不存在问题,这个问题与 db 中的行数有关吗?

【问题讨论】:

  • 不,问题是因为 PHP 版本。当您收到该错误时,您可能正在运行较旧的 PHP 版本。
  • 底线:您以错误的方式实现查询构建器。看看 Zend Framework 2/Db 他们如何实现where() 子句
  • 如果是数据库问题,那么您会收到数据库错误,而不是 PHP 错误。 PHP 不在乎数据库的能力。它只是发送查询并查看会发生什么。如果查询失败,PHP 不在乎。
  • /\[(.*)\]/i 最有可能写成/\[(.*?)\]/i。正如现在所写的那样,它消耗了第一个括号和最后一个括号之间的所有文本,因此出现错误。而不是str_replace 来逃避| 你应该使用preg_quote
  • 你能给出这个函数的原始字符串和预期的结果吗?

标签: php mysql regex


【解决方案1】:

问题在于 PHP 如何查询 MySQL,我不得不重新编写一些 SQL 查询,现在问题已经解决。

由 OP D_Guy13 在此 comment

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 2010-11-25
    相关资源
    最近更新 更多