1 (edited by chrizz 2006-07-26 12:07)

Topic: Strip (preg_match) bbcode of entire quotes, and quote on quote

I have this wich ereases a single quote, and it works fine for a singel quote like this:

$text = "[quote]asdf[/quote]
";

$text = preg_replace('#\[quote\]([^\[]*?)\[/quote\]#', '', $text);
$text = preg_replace('#\[quote=([^\[]*?)\](.*?)\[/quote\]#', '', $text);

echo $quote; // gives nothing...(works)

But it does not work for a quote "tree" like this:

$text = [quote][quote]asdf[/quote]
asdf[/quote]
$text = preg_replace('#\[quote\]([^\[]*?)\[/quote\]#', '', $text);
$text = preg_replace('#\[quote=([^\[]*?)\](.*?)\[/quote\]#', '', $text);

echo $quote; // This time it displays "[quote]asdf[/quote]
".

Re: Strip (preg_match) bbcode of entire quotes, and quote on quote

I believe you'll have to loop it. Something along the lines of while(preg_replace...

"Programming is like sex: one mistake and you have to support it for the rest of your life."

3 (edited by chrizz 2006-07-26 13:05)

Re: Strip (preg_match) bbcode of entire quotes, and quote on quote

Rickard wrote:

I believe you'll have to loop it. Something along the lines of while(preg_replace...

A while would not work since $text also contains the last message of the post, and is never "null". I believe it's like that anyway... What I want to do is to strip all quotes of a post, and only have the actual post left.

Re: Strip (preg_match) bbcode of entire quotes, and quote on quote

Well, in that case, you could just do:

$message = str_replace(array('[quote]', '[/quote]
'), '', $message);

Edit: Hmm, it appears the PunBB pre-parser inserts a linebreak in the middle of the code block above. Odd.

"Programming is like sex: one mistake and you have to support it for the rest of your life."

5 (edited by chrizz 2006-07-26 13:14)

Re: Strip (preg_match) bbcode of entire quotes, and quote on quote

Rickard wrote:

Well, in that case, you could just do:

$message = str_replace(array('[quote]', '[/quote]
'), '', $message);

Edit: Hmm, it appears the PunBB pre-parser inserts a linebreak in the middle of the code block above. Odd.

i don't believe thats removes the text within the actual quote.

[quote=nisse][quote=anders]Andres wrote this[/quote]
Nisse wrote this.[/quote]
I wrote this.

The function should return "I wrote this." only.

Re: Strip (preg_match) bbcode of entire quotes, and quote on quote

Use substr_replace?
http://be2.php.net/substr_replace

7 (edited by jmpy 2006-07-26 18:03)

Re: Strip (preg_match) bbcode of entire quotes, and quote on quote

Try this:

$text = preg_replace('#\[quote\](.*)\[/quote\]#', '', $text);
$text = preg_replace('#\[quote=([^\[]*?)\](.*)\[/quote\]#', '', $text);

EDIT: Actually, I just noticed that this doesn't work if you have quotes at the end of the string. Probably the best way is to use somekind of loop (as Richard said) that strips the nested quotes first. Here's one that does just that:

$old = null;
while ($text != $old)
{
    $old = $text;
    $text = preg_replace('#\[quote\]([^\[].*?)\[/quote\]#', '', $text);
    $text = preg_replace('#\[quote=([^\[]*?)\]([^\[].*?)\[/quote\]#', '', $text);
}

8 (edited by chrizz 2006-07-27 09:14)

Re: Strip (preg_match) bbcode of entire quotes, and quote on quote

jmpy, yeah, I changed that also. This is not very pretty but it works 99% of the times:

function stripBBCodeQuote($text)
{
    for ($i = 0; $i < 3; $i++)
        $text = preg_replace('#\[quote\]([^\[]*?)\[/quote\]#', '', $text);
    
    for ($i = 0; $i < 10; $i++)
        $text = preg_replace('#\[quote=([^\[]*?)\]([^\[]*?)\[/quote\]#', '', $text);

    return $text;
}

Your function is better though. With some modification I ended up with this that works perfect:

function stripBBCodeQuote($text)
{
    while ($text)
    {
        $text = preg_replace('#\[quote\]([^\[].*?)\[/quote\]#', '', $text);
        $text = preg_replace('#\[quote=([^\[].*?)\]([^\[]*?)\[/quote\]#', '', $text);
        if ($text == $old) break; else $old = $text;
    }
    return $text;
}

Re: Strip (preg_match) bbcode of entire quotes, and quote on quote

chrizz wrote:

i don't believe thats removes the text within the actual quote.

Ah, my mistake.

"Programming is like sex: one mistake and you have to support it for the rest of your life."

10 (edited by SirNotApearingOnThisForum 2006-08-22 13:24)

Re: Strip (preg_match) bbcode of entire quotes, and quote on quote

This is pretty old, but I'd just thought I'd mention that regular expressions have a recursive aspect, which may be useful if you need to deal with nested quotes.  For instance, this strips out all nested quotes without a loop:

$out = preg_replace('/\[quote(=[^\]]*?)?\]((.*)|(?R))\[\/quote\]/is', '', $text);