how to wrap 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

Post Reply
User avatar
cristti
Forum Newbie
Posts: 2
Joined: Sun Apr 29, 2007 11:41 am
Location: $_SERVER['REMOTE_ADDR']

how to wrap bbcode ?

Post by cristti »

hi there.. this is my first post here, and i'm sorry if i wrote on the wrong area..

i need a script which to delete the bbcode from my messages..

let's say that i have:

Code: Select all

[b]sample text[/b]
..

i need to return only

Code: Select all

sample text
.. without the bbcode..

so far, i thought i might use this:

Code: Select all

$de_inlocuit = array("[b]", "[/b]", "[u]", "[/u] . and so on ");
$post_curat = str_replace($de_inlocuit, "", $row_post['pagetext']);
but i have a problem if in the database will appear new bbcode.. [yt][/yt] as example..

how can i delete all the code between "[" "]" ?

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

Post by feyd »

[sic] for example may not be one you want to replace with nothing. I suggest using specific replacements, whatever they may be, instead of blanket ones. If you're worried about future tags you may add, you can build a plugin system that allows your code to discover which tags can be replaced.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

This will remove everything what's between [], but will keep if inside [] is another [ or ]

Code: Select all

$string = 'This is [i]some[/i] text with [url="http://../.com"]BB code[/url], but [this [u]should be[/u] kept]';
$pattern1 = '/\[([^\]^\[])+\]/i';
$pattern2 = '/\[\/(^\]^\[)+\]/i';

$result = preg_replace($pattern1, '', $string);
$result = preg_replace($pattern2, '', $result);

echo $result;
//Outputs: This is some text with BB code, but [this is should be kept]
Source: http://lv.php.net/manual/en/function.pr ... .php#50003

Edit: I agree with feyd :)
User avatar
cristti
Forum Newbie
Posts: 2
Joined: Sun Apr 29, 2007 11:41 am
Location: $_SERVER['REMOTE_ADDR']

Post by cristti »

thanks..

in fact, i want to post on one of my sites, the latest post from my forum. the site is out of the forum domain, and all i want is not to show to the users the bbcode, as the text is extracted from the database.

i searched on php.net after something to help me, but didn't find..

thanks a lot, i'll try ur code.. it should be more than enough :)
Post Reply