duanjing7651 2016-01-02 12:13 采纳率: 0%
浏览 75
已采纳

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条回答 默认 最新

  • dongqiu5184 2018-08-28 10:54
    关注

    Just to close this:

    Simply use shortcodes instead.

    wordpress reference for add_shortcode

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!