我如何加强输入使用preg_match/replace regex


How can I harden input used with preg_match/replace regex?

我正在使用一个PHP脚本查找

var $_plgCode       = "#{comment(.*?) contentid=(.*?) option=(.*?) contenttitle=(.*?)}#i";

,然后在:

中使用。
preg_match_all($this->_plgCode, $_body, $matches);

$_body = preg_replace($this->_plgCode, $output, $_body);

问题是contenttitle可以包含用户输入,而且根本没有被加固——所以很多事情会破坏它,比如如果用户输入},例如。

用户输入需要在contenttitle上进行什么样的转义以确保它不会破坏REGEX?

使用preg_quote