wordpress 中的 preg_replace 停在 html 标签处
a preg_replace in wordpress stops at html tags
我在我的 functions.php 中为我的 wordpress 博客写了一个 preg_replace 供我女朋友使用 "shortcode",如下所示:
[spoiler id="1" show="show me the list" hide="hide the list"]test1
test2<ul>test3
<li>list item 1</li>
<li>list item 2</li>
</ul>
[/spoiler]
我的preg_replace在functions.php中是这样的:
function BUFU_add_spoilers ($content)
{ global $post;
$pattern = "/\[spoiler id=\"(.*?)\" show=\"(.*?)\" hide=\"(.*?)\"\](.*?)\[\/spoiler\]/is";
$replacement ='<br><div align="right" style="margin: -58px 0 0 0"><a id="button_show_%SPOILID%_" onclick="document.getElementById(\'spoiler_%SPOILID%_\').style.display=\'block\'; document.getElementById(\'button_hide_%SPOILID%_\').style.display=\'\'; document.getElementById(\'button_show_%SPOILID%_\').style.display=\'none\';"><h5>[]</h5></a></div><div align="right"><a id="button_hide_%SPOILID%_" style="display: none" onclick="document.getElementById(\'spoiler_%SPOILID%_\').style.display=\'none\'; document.getElementById(\'button_hide_%SPOILID%_\').style.display=\'none\'; document.getElementById(\'button_show_%SPOILID%_\').style.display=\'\';"><h5>[]</h5></a></div><span id="spoiler_%SPOILID%_" style="display: none" class="spoilerBox"></span>';
$content = preg_replace($pattern, $replacement, $content);
$content = str_replace("%SPOILID%", $post->ID, $content);
return $content;
}
add_filter('the_content', 'BUFU_add_spoilers', 5);
如果 "shortcodes" 之间只有文本,则一切正常,但它不适用于上面的示例。剧透将包括开头 "<ul>"
(或开头 "<table>"
之前的所有内容),因此 test1 和 test2 在剧透中,test3 不是。
我只是不知道为什么。我在线测试了 preg_replace,它工作得很好。
我认为这与 wordpress 添加过滤器和内容以处理文本的方式有关,但我无法弄清楚...
结果只是一个空 ... class="spoilerBox"></span>
后跟列表..
如果有人能帮我解决这个问题,将不胜感激!
我真的为这个问题发疯了!
提前致谢,
布福
只是为了关闭这个:
只需使用短代码即可。
我在我的 functions.php 中为我的 wordpress 博客写了一个 preg_replace 供我女朋友使用 "shortcode",如下所示:
[spoiler id="1" show="show me the list" hide="hide the list"]test1
test2<ul>test3
<li>list item 1</li>
<li>list item 2</li>
</ul>
[/spoiler]
我的preg_replace在functions.php中是这样的:
function BUFU_add_spoilers ($content)
{ global $post;
$pattern = "/\[spoiler id=\"(.*?)\" show=\"(.*?)\" hide=\"(.*?)\"\](.*?)\[\/spoiler\]/is";
$replacement ='<br><div align="right" style="margin: -58px 0 0 0"><a id="button_show_%SPOILID%_" onclick="document.getElementById(\'spoiler_%SPOILID%_\').style.display=\'block\'; document.getElementById(\'button_hide_%SPOILID%_\').style.display=\'\'; document.getElementById(\'button_show_%SPOILID%_\').style.display=\'none\';"><h5>[]</h5></a></div><div align="right"><a id="button_hide_%SPOILID%_" style="display: none" onclick="document.getElementById(\'spoiler_%SPOILID%_\').style.display=\'none\'; document.getElementById(\'button_hide_%SPOILID%_\').style.display=\'none\'; document.getElementById(\'button_show_%SPOILID%_\').style.display=\'\';"><h5>[]</h5></a></div><span id="spoiler_%SPOILID%_" style="display: none" class="spoilerBox"></span>';
$content = preg_replace($pattern, $replacement, $content);
$content = str_replace("%SPOILID%", $post->ID, $content);
return $content;
}
add_filter('the_content', 'BUFU_add_spoilers', 5);
如果 "shortcodes" 之间只有文本,则一切正常,但它不适用于上面的示例。剧透将包括开头 "<ul>"
(或开头 "<table>"
之前的所有内容),因此 test1 和 test2 在剧透中,test3 不是。
我只是不知道为什么。我在线测试了 preg_replace,它工作得很好。
我认为这与 wordpress 添加过滤器和内容以处理文本的方式有关,但我无法弄清楚...
结果只是一个空 ... class="spoilerBox"></span>
后跟列表..
如果有人能帮我解决这个问题,将不胜感激! 我真的为这个问题发疯了!
提前致谢,
布福
只是为了关闭这个:
只需使用短代码即可。