0

Say I have a document like:

 [b]blah[/b]
 [img]Thisismyimage.png[/img]

How can I make it so that I completely remove all of the BBcode tags. And also remove all the text from between the [img] tags.

If it helps at all I am using an IPB board. If any knows of a way to easily to parse the BBcode that would be great, however, I am perfectly happy with just removing it.

endy
  • 3,872
  • 5
  • 29
  • 43

1 Answers1

2

Parsing BBcode is pretty much a solved task: http://pear.php.net/package/HTML_BBCodeParser - And that would also be the more advisable path for removing (for simplicity just apply strip_tags() afterwards).

But for removing a limited set of syntax constructs, you could use a very simple regex:

 $text = preg_replace('#\[img].*?\[/img]|\[/?\w+.*?]#', '', $text);
mario
  • 144,265
  • 20
  • 237
  • 291
  • The BBcode parsing isn't that easy given that I am developing on an IPB board. The preg_replace is perfect though. Is there any way I can use preg replace to just remove all bbcode? – endy Jun 07 '11 at 03:03
  • This example should remove all singular bbcode tags, plus that one special [img] case. It will not remove any bbcode tags with attributes `[xy a=b]` for example. You'll have to try if that's usable in your case. – mario Jun 07 '11 at 03:04
  • I'll be honest I really cannot wrap my head around regular expressions I would GREATLY appreciate it if you could provide and example of how to also remove bb code tags that have attributes. – endy Jun 07 '11 at 03:10
  • I did add another `.*?` catch-all for optional bbcode attributes. That's not a very professional approach (could be more exact), but I don't know your actual used bbcode syntax.. Could be problematic if you have spurious square brackets in the text. Else worth a try. – mario Jun 07 '11 at 03:15