preg_replace_callback regex语法不正确



当我打开sql查询日志来搜索Magento时,我发现了一篇对Magento的核心query函数进行了修改的帖子,如下所示:

public function query($sql, $bind = array())
{
$this->_debugTimer();
try {
$sql = (string)$sql;
if (strpos($sql, ':') !== false || strpos($sql, '?') !== false) {
$this->_bindParams = $bind;
$sql = preg_replace_callback('#((['"])((2)|((.*?[^])2)))#', array($this, 'proccessBindCallback'), $sql);
$bind = $this->_bindParams;
}
$code = 'SQL: ' . $sql . "rn";
if ($bind) {
$code .= 'BIND: ' . print_r($bind, true) . "rn";
}
$this->_debugWriteToFile("[".date('Y-m-d H:i:s')."] ".$code);
$result = parent::query($sql, $bind);
}
catch (Exception $e) {
$this->_debugStat(self::DEBUG_QUERY, $sql, $bind);
$this->_debugException($e);
}
$this->_debugStat(self::DEBUG_QUERY, $sql, $bind, $result);
return $result;
}

我不明白的是,为什么这里需要preg_replace_callback,当我把上面的代码放在编辑器中时,语法中包含preg_replace_callback的那一行出现了什么问题?

有什么建议吗?

您在[^\]附近的regexp中有错误,这是正确的

'#((['"])((2)|((.*?[^\])2)))#'

对于php字符串,您还需要转义一些字符

   $re = "/#(([\'"])((2)|((.*?[^\\])2)))#/";  
   $str = "";` 
   preg_match($re, $str, $matches);

我经常使用https://regex101.com/用于检查和调试regexp。

相关内容