preg_replace解析错误:函数中的语法错误

时间:2012-12-08 17:09:27

标签: php preg-replace

我正在尝试将GeSHi语法荧光笔集成到我的博客中。

我的代码中出现语法错误。我对PHP代码不太了解,因此寻求帮助来纠正语法。

我的代码是:

private function _renderCode($string)
{
    return preg_replace('/<listing (.*?)>(.*?)</listing>/es',
                '$this->highlightString('\2', '\1')', 
                $string);
}

错误讯息为:

Parse error: syntax error, unexpected '\' (T_NS_SEPARATOR)

2 个答案:

答案 0 :(得分:1)

尝试'$this->highlightString(\'\\2\', \'\\1\')',

答案 1 :(得分:0)

查看语法高亮:

'$this->highlightString('\2', '\1')'

您需要在单引号字符串中转义单引号。

'$this->highlightString(\'\2\', \'\1\')'

(顺便提一下,首选的形式是'$this->highlightString(\'$2\', \'$1\')'。)