我在下面有一个简单的(BBCode)PHP 代码,用于将代码插入评论/帖子。
function highlight_code($str) {
$search = array(
'/\[code=(.*?),(.*?)\](((?R)|.)*?)\[\/code\]/is',
'/\[quickcode=(.*?)\](((?R)|.)*?)\[\/quickcode\]/is'
);
$replace = array(
'<pre title="$2" class="brush: $1;">$3</pre>',
'<pre class="brush: $1; gutter: false;">$2</pre>'
);
$str = preg_replace($search, $replace, $str);
return $str;
}
我想要做的是在这些位置插入函数:
$replace = array(
'<pre title="$2" class="brush: $1;">'.myFunction('$3').'</pre>',
^here
'<pre class="brush: $1; gutter: false;">'.myFunction('$2').'</pre>'
^here
);
根据我在 SO 上阅读的内容,我可能需要使用preg_replace_callback()
或 e-modifier,但我无法弄清楚如何去做;我对正则表达式的了解不是很好。将不胜感激一些帮助!