Php preg_replace_回调返回空字符串?
我有一个WordPress插件(它的部分功能),可以在帖子中运行php,并将输出放在屏幕上的适当位置。代码如下所示:Php preg_replace_回调返回空字符串?,php,Php,我有一个WordPress插件(它的部分功能),可以在帖子中运行php,并将输出放在屏幕上的适当位置。代码如下所示: add_filter('the_content', 'twomb_autocode_do_php', 0); //Parse php in the post with [php] ... [/php] tags. function twomb_autocode_do_php($content) { $content = preg_replace_callback('/\[
add_filter('the_content', 'twomb_autocode_do_php', 0); //Parse php in the post with [php] ... [/php] tags.
function twomb_autocode_do_php($content) {
$content = preg_replace_callback('/\[php\]((.|\n)+)\[\/php\]/', 'twomb_autocode_exec_php', $content); //I know, weird regex.
return $content;
}
function twomb_autocode_exec_php($matches) {
ob_start();
eval($matches[1]);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
然而,当像[php]这样简单的东西发出“hi!”的回声时;[/php]
在帖子中,返回的内容为“”(空)。如果没有包含php标记,则内容显示良好(尽管显然没有包含php)。我到处寻找返回空字符串的preg\u replace\u callback
,我发现php上唯一一个伪造的bug,无效的unicode字符导致preg\u replace\u callback
返回空字符串,尽管我几乎肯定我的字符串根本没有任何unicode字符
-迈克尔
另外,对格式设置很抱歉--我的屏幕阅读器与此站点上的编辑器完全不兼容。是否尝试
var\u dump
您的$匹配项
?我打赌与那些ob.*
可能是嵌套匹配有关。你不需要它。执行/\[php\]([^\]]+)\[\/php\]/
。也就是说,如果我理解正确的话,这个代码就是一个巨大的安全漏洞。几乎没有理由使用eval()