Home | Forums | Articles | F@H
Help | Search | Members | Calendar | Forum Map | Cybervillage | Archive
Welcome Guest ( Log In | Register | Resend Validation Email )


>  IceTeks Forums -> Technology made cool -> Programming


  Archived - No replies allowed - start new thread insteadStart new topicStart Poll

>  BBCode Help improving script
Track this topic | Email this topic | Print this topic
eduardor2k
Posted: Mar 19 2006, 04:50 PM
Quote Post

Icicle

Group Icon

Group: IceTeks Newbies
Posts: 8
Member No.: 1 201
Joined: 19-March 06
Ice Cubes: 30



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
[quote]1a parte[quote]2a parte [/quote]3a parte[/quote]


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 <br />
       $post_with_br = nl2br($post_bbcode_treated);


       echo $post_with_br;

?>


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.
PMEmail Poster Top
Red Squirrel
Posted: Mar 19 2006, 07:01 PM
Quote Post

ZOMG!

Group Icon

Group: Admins
Posts: 13 964
Member No.: 1
Joined: 18-December 02
Ice Cubes: 163183



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.


--------------------
user posted image
http://www.redsquirrel.me my blog
http://www.uovalor.com AoS/ML Ultima Online Shard
PMEmail PosterUsers Website Top
eduardor2k
  Posted: Mar 19 2006, 07:22 PM
Quote Post

Icicle

Group Icon

Group: IceTeks Newbies
Posts: 8
Member No.: 1 201
Joined: 19-March 06
Ice Cubes: 30



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

This post has been edited by eduardor2k on Mar 19 2006, 07:31 PM
PMEmail Poster Top
Red Squirrel
Posted: Mar 19 2006, 09:38 PM
Quote Post

ZOMG!

Group Icon

Group: Admins
Posts: 13 964
Member No.: 1
Joined: 18-December 02
Ice Cubes: 163183



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.


--------------------
user posted image
http://www.redsquirrel.me my blog
http://www.uovalor.com AoS/ML Ultima Online Shard
PMEmail PosterUsers Website Top
Streety
Posted: May 21 2006, 11:19 AM
Quote Post

Liquid Nitrogen Blaster

Group Icon

Group: Ice Age Members
Posts: 185
Member No.: 475
Joined: 16-August 04
Ice Cubes: 5



I posted that tutorial when I was young and naive. laugh.gif 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.


--------------------
user posted image
PMEmail Poster Top
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:
« Next Oldest | Programming | Next Newest »

Topic Options Archived - No replies allowed - start new thread insteadStart new topicStart Poll

 



[ Script Execution time: 0.0362 ]   [ 13 queries used ]   [ GZIP Enabled ]

< Home | Forums | Contact >