wordpress中的preg_replace在html标签处停止
I have written a preg_replace in my functions.php for my wordpress blog for my girlfriend to use a "shortcode" like the following:
[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]
My preg_replace in the functions.php is the following:
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%_$1" onclick="document.getElementById(\'spoiler_%SPOILID%_$1\').style.display=\'block\'; document.getElementById(\'button_hide_%SPOILID%_$1\').style.display=\'\'; document.getElementById(\'button_show_%SPOILID%_$1\').style.display=\'none\';"><h5>[$2]</h5></a></div><div align="right"><a id="button_hide_%SPOILID%_$1" style="display: none" onclick="document.getElementById(\'spoiler_%SPOILID%_$1\').style.display=\'none\'; document.getElementById(\'button_hide_%SPOILID%_$1\').style.display=\'none\'; document.getElementById(\'button_show_%SPOILID%_$1\').style.display=\'\';"><h5>[$3]</h5></a></div><span id="spoiler_%SPOILID%_$1" style="display: none" class="spoilerBox">$4</span>';
$content = preg_replace($pattern, $replacement, $content);
$content = str_replace("%SPOILID%", $post->ID, $content);
return $content;
}
add_filter('the_content', 'BUFU_add_spoilers', 5);
All works just fine if there is only text in between the "shortcodes", but it somehow does not work for the example above. The spoiler will include everything up to the opening "<ul>"
(or an opening "<table>"
for that matter), so test1 and test2 are in the spoiler, test3 is NOT.
I just have no idea why. I tested the preg_replace online and it worked just fine.
I reckon it has something to do with the way wordpress adds filters and stuff to work on the text, but I can't figure it out...
The result is just an empty ... class="spoilerBox"></span>
followed by the list..
If anyone can help me with this, it would be greatly appreciated! I'm really going nuts over this problem!
Thanks in advance,
BUFU
- 点赞
- 写回答
- 关注问题
- 收藏
- 复制链接分享
- 邀请回答
1条回答
为你推荐
- 如何在wordpress中突出显示搜索内容中的特定单词
- php
- regex
- wordpress
- jquery
- 1个回答
- 以编程方式在Wordpress中编辑PHP文件
- php
- wordpress
- 1个回答
- WordPress Image Gallerys的自定义HTML只输出一半的功能
- php
- wordpress
- 2个回答
- preg_match查找并替换字符串模式
- php
- html-parsing
- 5个回答
- 如何在PHP中使用preg_replace匹配俄语单词?
- php
- regex
- utf-8
- 3个回答
- 将p标签放在拉出的内容上 - Wordpress
- html
- php
- wordpress
- 1个回答
- 在Wordpress中显示图像后添加图像URL
- php
- regex
- wordpress
- 2个回答
- Wordpress插件不输出HTML
- plugins
- php
- wordpress
- 2个回答
- 转换后的数组元素到字符串不能与preg_match一起使用,即使这个相同的字符串与同一个preg_match一起使用也是如此
- string
- php
- regex
- arrays
- 1个回答
- 如何在wordpress主题设置面板中使用preg_replace来获取帖子标题或标签
- php
- wordpress
- 2个回答
- 如何在wordpress的feed中显示没有html标签的内容?
- php
- regex
- wordpress
- 1个回答
- wordpress中的preg_replace在html标签处停止
- filter
- php
- wordpress
- 1个回答
- WordPress nav_menu - 按菜单名称过滤带条件语句的输出?
- html
- php
- menu
- wordpress
- 1个回答
- preg_replace第二个参数($ replacement)不合适
- html
- php
- regex
- wordpress
- 1个回答
- str_replace()有3个不同的替换
- php
- wordpress
- 2个回答
- 警告:preg_replace_callback():需要参数2,才能成为有效的回调
- php
- wordpress
- 1个回答
- 在wordpress帖子中拉出blockquote
- php
- wordpress
- 1个回答
- 数组:使用preg_replace和排序过滤[PHP - WordPress]
- sorting
- php
- arrays
- 1个回答
- Preg_Replace和Capture Value? PHP
- php
- regex
- 1个回答
- 使用preg_replace和FOR循环替换PHP关键字
- loops
- php
- wordpress
- 1个回答