Forum Moderators: coopster

Message Too Old, No Replies

PHP - get whatever is NOT inside BBcode tags

         

imagined

8:54 pm on Oct 7, 2008 (gmt 0)

10+ Year Member



I'm creating this custom tags function. it uses bbcode style, that is, using square brackets [ ] instead of pointy brackets < >

I can already isolate what's inside the square bracket tags so i can format them as i wish.

for example: php tag is color coded, i apply htmlentities to html tags, tips tag has a yellowish background

but now i need to select whats NOT inside the tags so i can apply nl2br so line breaks can be displayed on the browser.

i cant just apply nl2br to the whole string because then i would get double spacing on the color coded php or on htmlentities

is there a regular expression on preg_split or someway i can just get the parts that is not surrounded by the bbcode tags?

rob7591

10:30 pm on Oct 7, 2008 (gmt 0)

10+ Year Member



preg_replace("/\[.+\]/", "", $text) should take out all the bbcode, but that probably wouldn't work because you wouldn't know what tags went where after you nl2br that string.

I would apply the formatting of the bbcode to the string, then nl2br the new formatted HTML string.

rob7591

10:39 pm on Oct 7, 2008 (gmt 0)

10+ Year Member


Found a new way, i think could help if you work on it.
you can use the PREG_SPLIT_DELIM_CAPTURE constant to add the delimiters to the split array as well, so you can do:

$text = '[B]Hello[/B] This is a
line [U]break[/U]';
$array = preg_split('/\[.+\]/', $text, -1, PREG_SPLIT_DELIM_CAPTURE);

/* I didn't test, but this is my guess:
$array =
Array
{
[0] =>
[1] => [B]
[2] => Hello
[3] => [/B]
[4] => This is a
line
[5] => [U]
[6] => break
[7] => [/U]
[8] =>
*/