Page 1 of 1

how to wrap bbcode ?

Posted: Sun Apr 29, 2007 11:51 am
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..

Posted: Sun Apr 29, 2007 12:17 pm
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.

Posted: Sun Apr 29, 2007 12:23 pm
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 :)

Posted: Sun Apr 29, 2007 12:37 pm
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 :)