Strip BBCode via RegEx

醉酒当歌 提交于 2019-11-30 09:52:43

问题


I am trying to setup a regex that will detect [quote]???[/quote] and will remove it.

This is what I have but it is not working:

$post['body'] = preg_replace("/\[quote\](.+?)\[\/quote\]/is", '', $post['body']);

Can anyone point me in the right direction?

I also want to remove any line brakes before or after the [quote]???[/quote].


回答1:


Here's my test with your script:

$text = "I am trying to setup a regex that will detect [quote]???[/quote] and will remove it.\r\nThis is what I have but it is not working:";
$sentences = preg_replace("/\[quote\](.+?)\[\/quote\]/is", '', $text);
echo '<pre>'.print_r($sentences, true).'</pre>';

And my Output:

I am trying to setup a regex that will detect  and will remove it.
This is what I have but it is not working:

You can see: [quote]???[/quote] is removed.

I think your problem is anywhere else. Check the Value of $post['body']!

Maybe it is a misspelling and you meen $_POST['body']?




回答2:


Just figured out my own issue.

$post['body'] = preg_replace("/\[quote\](.+?)\[\/quote\]/is", '', $post['body']); $body = trim(rtrim($post[0]['body']));



来源:https://stackoverflow.com/questions/5759265/strip-bbcode-via-regex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!