2016-05-16 22 views
1

雖然我正在尋找的Magento在SQL查詢日誌轉彎,我發現修改後,以如下Magento的核心query功能:preg_replace_callback正則表達式的語法不正確

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這一行的問題是什麼,它顯示了報價突出顯示?

有什麼建議嗎?

+0

轉義單引號。看到這裏的顏色突出顯示? '''']'這個單引號關閉了你的PHP字符串封裝。 – chris85

+0

你說得對,但是在這之後如果我運行我的代碼,它會在日誌中出現錯誤''''在同一行中出現' –

+0

如果你有Magento的源代碼與你一起試用這個,把你的lib/Varien/Db/Adapter/Pdo/Mysql.php替換掉現有的'query'函數,然後運行Magento主頁,你會知道我得到了什麼錯誤。 –

回答

0

你有錯在附近的正則表達式[^ \],這是對

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

爲PHP字符串,你還需要逃避的某些字符

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

我見於Ger.Offen使用https://regex101.com/進行檢查和調試正則表達式。

+0

這適用於感謝您的答案。 –