【问题标题】:Strip BBCode via RegEx通过 RegEx 剥离 BBCode
【发布时间】:2011-04-22 19:31:18
【问题描述】:

我正在尝试设置一个正则表达式来检测 [quote]???[/quote] 并将其删除。

这是我拥有的,但它不起作用:

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

谁能指出我正确的方向?

我还想在 [quote]???[/quote] 之前或之后移除所有线闸。

【问题讨论】:

  • 刚刚想通了:$post['body'] = preg_replace("/\[quote\](.+?)\[\/quote\]/is", '', $post['body']); $body = trim(rtrim($post[0]['body']));

标签: php parsing bbcode


【解决方案1】:

这是我对你的脚本的测试:

$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>';

我的输出:

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

你可以看到:[quote]???[/quote]被删除了。

我认为您的问题出在其他任何地方。检查 $post['body'] 的值!

也许是拼写错误,而您的意思是 $_POST['body']?

【讨论】:

    【解决方案2】:

    刚刚发现自己的问题。

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

    【讨论】:

    • trim() 将删除字符串两边的空格。所以不需要再添加一个rtrim。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    相关资源
    最近更新 更多