[SOLVED] Help with removing bbCode

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[google]man page grep[/google]
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

google for tutorials

check out the one at ZEND if you come across it, very helpful.

also search for "redmonkey" here, he has helped alot of people with regExs
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

In the example given in the fist post there is actually an obvious pattern in that there is an opening tag '[bbtag:uid]' and a closing tag '[/bbtag:uid]' so it would be quite easy to apply a regex to find the opening tag then everything in between and up to the matching closing tag then replace that with just the text in between.

Code: Select all

$data[$i]['topic_text'] = preg_replace('/\[([^\]]+)](.*?)\[\/\\1]/s', '$2', $data[$i]['topic_text']);
However there are a few special cases (color= etc..) so the regex needs to be extended slightly (untested)....

Code: Select all

$data[$i]['topic_text'] = preg_replace('/\[(\w+)(?:=[^:]+)?(:[^\]]+)](.*?)\[\/\\1\\2]/s', '$3', $data[$i]['topic_text']);
So I reckon it could be done with a single (or possibly two) regex without too much hassle.
Post Reply