【发布时间】:2014-01-06 17:05:04
【问题描述】:
我有一个可能包含短代码的字符串,例如“这是一个带有 [anyshortcode1] 的字符串,这是 [anyshortcode2]”。我想显示带有在它们所在位置运行的简码的字符串。当我回显这个字符串时,它不会执行短代码,它只是将它们打印出来,并将它们留在括号中。我尝试使用如下代码解决此问题:
$string_with_shortcodes = "this is a string with [anyshortcode1] and this is [anyshortcode2]";
$pattern = get_shortcode_regex();
preg_match_all("/$pattern/",$string_with_shortcodes, $matches);
foreach ($matches[0] as $short) {
$string_with_shortcodes = str_replace($short, 'do_shortcode("' . $short . '")', $string_with_shortcodes);
}
eval("\$string_with_shortcodes = \"$string_with_shortcodes\";");
echo $string_with_shortcodes;
这成功完成了字符串替换,但导致 eval 函数出错:
解析错误:语法错误,意外的 T_STRING
我是否使用了 eval 函数错误?或者有没有更简单的方法来完成从字符串运行短代码?
【问题讨论】:
标签: php wordpress eval shortcode