Remove bbcode from string

Hi guys,

How can I remove all text contained within the

[/quote] brackets from the following string?


$text = "Hello my name is Peter and I said [quote=myusername112]Hello!
";

Test result should return:

Hello my name is Peter and I said 

function stripBBCode($text_to_search) {
 $pattern = '|[[\\/\\!]*?[^\\[\\]]*?]|si';
 $replace = '';
 return preg_replace($pattern, $replace, $text_to_search);
}

echo stripBBCode($text);

I just found this function somewhere but it works great for me when I’m displaying latest posts from vbulletin and phpbb in my homepage.

Thanks, but that will only remove the quote tags. Is there a way of removing the text within the quote tags as well?

A possible one is:

~\\[([^\\]]+?)(=[^\\]]+?)?\\](.+?)\\[/\\1\\]~

Or, if you’re a beginner/novice in terms of RegEx, here’s a more beginner friendly approach:


<?php
$OpenBrace = '\\[';
$CloseBrace = '\\]';
$NotACloseBrace = "[^{$CloseBrace}]";
$Multiple = '+?';
$Optional = '?';
$Anything = "(.{$Multiple})";
$StartTag = "{$OpenBrace}({$NotACloseBrace}{$Multiple})(={$NotACloseBrace}{$Multiple}){$Optional}{$CloseBrace}";
$EndTag = "{$OpenBrace}/\\\\1{$CloseBrace}";
$FullPattern = "~{$StartTag}{$Anything}{$EndTag}~";
$String = "Hello my name is Peter and I said 
[quote="myusername112"]
Hello!
[/quote]
, and someone once said 
> Farewell!
";
$String = preg_replace($FullPattern, '', $String);
echo $String;
?>

Thanks, that worked a treat!