Page 1 of 1

BBCode Help improving script

Posted: Sun Mar 19, 2006 3:50 pm
by Anonymous
Hi to everyone, i've searched the internet for bbcode 2 html script, and i've found this website with a great tutorial, i was searching a script that can workaround this litle bug.

Code: Select all

[quote]1a parte[quote]2a parte [/quote]3a parte[/quote][code]

When i use the following code:
[code]
<?php
$code_treated = "[quote]1a parte[quote]2a parte [/quote]3a parte[/quote]";

//Arrays for the bbCode replacements
        $bbcode_regex = array(0 => '/[b](.+?)[/b]/s',
                                                1 => '/[i](.+?)[/i]/s',
                                                2 => '/[u](.+?)[/u]/s',
                                                3 => '/[quote](.+?)[/quote]/s',
                                                4 => '/[quote=(.+?)](.+?)[/quote]/s',
                                                5 => '/[url](.+?)[/url]/s',
                                                6 => '/[url=(.+?)](.+?)[/url]/s',
                                                7 => '/[img](.+?)[/img]/s',
                                                8 => '/[color=(.+?)](.+?)[/color]/s',
                                                9 => '/[size=(.+?)](.+?)[/size]/s');

        $bbcode_replace = array(0 => '<b>$1</b>',
                                                1 => '<i>$1</i>',
                                                2 => '<u>$1</u>',
                                                3 => '<table class="quote"><tr><td>Quote:</td></tr><tr><td class="quote_box">$1</td></tr></table>',
                                                4 => '<table class="quote"><tr><td>$1 said:</td></tr><tr><td class="quote_box">$2</td></tr></table>',
                                                5 => '<a href="$1">$1</a>',
                                                6 => '<a href="$1">$2</a>',
                                                7 => '<img src="$1" alt="User submitted image" title="User submitted image"/>',
                                                8 => '<span style="color:$1">$2</span>',
                                                9 => '<span style="font-size:$1">$2</span>');

        ksort($bbcode_regex);
        ksort($bbcode_replace);

        //preg_replace to convert all remaining bbCode tags
        $post_bbcode_treated = preg_replace($bbcode_regex, $bbcode_replace, $code_treated);

//Convert new lines to 

        $post_with_br = nl2br($post_bbcode_treated);


        echo $post_with_br;

?>
[code]

This will result, that the first [quote] wil work with the first [/quote] tag and dismiss the other [quote] tag, but it should do this, the first [quote] with the last [/quote], and the second [quote] with the second [/quote].

Someona has some ideas how to fix this.

Thanks. 

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:4240, old post ID:34313[/size][/color]

BBCode Help improving script

Posted: Sun Mar 19, 2006 6:01 pm
by Red Squirrel
Funny as I'm working on a forum script, and this is part of a challenge that I put on the back burner for now: nested quotes and other nested bbcode. The regular expressions on forum scripts such as IPB look rather complex and I dont want to just steal code either so got to look at it and try to understand it, once I get to the bbcode section.

The easy way is instead of using regular expressions is simply replace.

So [QUOTE ] is replaced with <div class="quote"> and [/QUOTE ] with </div>

But when you want to add stuff like the author it gets more complex then that so this is really just a temporary workaround.

So your best bet is probably to look at the parser of IPB or phpbb or other script and try to understand the preg_replace regex string.

I also remember reading something about "Greedy" replacing, which I think has to do with nested stuff like this. Something to do more research on perhaps.

Archived topic from Iceteks, old topic ID:4240, old post ID:34314

BBCode Help improving script

Posted: Sun Mar 19, 2006 6:22 pm
by Anonymous
My workaround is still theorical.

I should count the position of the tags, and replace them in fuction of their position:

Example...
---[item1]
-------[item2][/item2]
---[/item1]
---[item3]
---[/item3]
---[item4]
-------[item5][/item5]
-------[item6][/item6]
---[/item4]

In this case i should do in 2 passes, the first one will replace item2,5 and 6 and in the next pass it will replace item1,3,4.

The passes thing it's easy, it's only needed a loop like for or while but the tricky part is analyse how many passes are needed and what position of the string they should work.

pass 1 -> should replace between chars 21 and 59, 96 and 115 (it's an example)
pass 2 -> the same
-
-
pass n -> this one doesn't need that, because it only replaces whats left.

I would like your opinión on this method.

I think using this scheme, solves every problem of replacing every tag with it's correct replacement, but we would also need a way to verify that the user is writing correctly and didn't miss to write any tag (This would make this sistem not to work correcty, but this happens in other systems too).

Adding something to this problem, it could be possible to add a way to verify the structure, and if something is missing, it would the tag where it belongs

I found that your workaround solves the problem, why you are not happy with that method.

Thanks, Eduardo

Archived topic from Iceteks, old topic ID:4240, old post ID:34315

BBCode Help improving script

Posted: Sun Mar 19, 2006 8:38 pm
by Red Squirrel
could work but it does sound like a possibility for lot of bugs, like if the user screws it up. Could even go as far as messing up the the post screen layout. Think what is really needed is a very creative preg_replace that will do it in one shot. regex is not my main skill so can't really help with that part, I'll have to try stuff myself once I get to doing this on my bb system.

Archived topic from Iceteks, old topic ID:4240, old post ID:34318

BBCode Help improving script

Posted: Sun May 21, 2006 11:19 am
by Streety
I posted that tutorial when I was young and naive. :lol: I dread to think how many problems there are with it.

Now that I'm older, a little more experienced and a lot more busy I know the value of using the labours of other people. More specifically in this case the work of one person, Stijn de Reede, who posted a BBCode parser on the PEAR site.

Although I haven't yet used it, if I was putting together a forum script that's where I would start today. It might not be as fast as is possible but it's released as stable and given that he has been working on it since 2003 it seems fairly likely.

P.S. Hi to everyone, seems like an age since I last visited the site. I'll probably show my face again once summer really arrives.

Archived topic from Iceteks, old topic ID:4240, old post ID:35053