Here is a BB Code that I would like to convert to HTML and then Back if necessary on a bulletin board. I have a JavaScript that prompts for 5 pairs of key-value and yeilds:

[TABLE BD=5 BDC=blue BGC=azure CP=5 CS=3]

I would like to turn this code generated by the 5 prompts to the following so it will appear in the preview of posted message.

<TABLE BORDER = "5" BORDERCOLOR = "blue" BGCOLOR= "azure" CELLPADDING = "5" CELLSPACING = "3">

I almost had it I think but it missed a < and then the more I messed with it the worse it got using preg_replace.

I can easily convert [TABLE] {/TABLE to <TABLE> </TABLE> with this conversion:

$txt = preg_replace( "#\[table\](.+?)\[/table\]#is", "<TABLE>\\1</table>", $txt );
// and unconvert with
$txt = preg_replace( "#(<table>)(.+?)(</table>)#is","[table\]\\1\[/table\]", $txt );
// I can also convert these two td attributes with
$txt = preg_replace( "#\[td c=(\S+?)\s*\](.+?)\[/td\]#is", "<td colspan=\"\\1\">\\2</td>", $txt );
$txt = preg_replace( "#\[td r=(\S+?)\s*\](.+?)\[/td\]#is", "<td rowspan=\"\\1\">\\2</td>", $txt );
// and unconvert with
$txt = preg_replace( "#(<td colspan=\"(.+?)\">)(.+?)(</td>)#is","[td c=\\2\]\\3\[/td\]", $txt );
$txt = preg_replace( "#(<td rowspan=\"(.+?)\">)(.+?)(</td>)#is","[td r=\\2\]\\3\[/td\]", $txt );

I just can't seem to get how to convert 5 attributes to HTML and then back to BB Code, what do I need to do to make it work?

    I've found that the best way to do this, is to just save the BBCode in your database, and convert it to HTML when a user views that BBCode enabled content.

    This way your not encoding / decoding.

      When you enter a BB Code into the text box it is converted into HTML when a user presses the add post or submit post or whatever, it then takes the code and through the program will converts it to HTML because that is how the message is displayed. this is a BB Code that I am programming, not one that has already been programmed the IPB or Invision board 1.2f or 1.3 does not have this code so I am building one, and when you enter the editing function the HTML that you see in the message area is then converted back to BB Code so saving a BB Code that has not been converted to HTML will only dislpay in the message as BB Code, and not the actual messages as intended, this is why I am asking the question in the first place. If you were to try to write BB Code as a program and try to view it in your browser wuold you get the desired result...NO because browsers only understand HTML not BB Code!

        Your spacing is very important so here is a code that works both to convert the BB Code to HTML to view in a posted message for the IPB board, and the code to unconvert the HTML back into BB Code for editing. The JavaScript generated code is as follows and the code to convert the generated tag are below.
        [TABLE bd = 5 bgc = gray bdc = blue cp = 2 cs = 2]

        // Convert post to html for display
        $txt = preg_replace( "#\[TABLE bd =(.+?)bgc =(.+?)bdc =(.+?)cp =(.+?)cs =(.+?)\](.+?)\[/TABLE\]#is",
         "<table border =\\1bgcolor =\\2bordercolor =\\3cellpadding =\\4cellspacing =\\5>\\6</table>", $txt );
        // Convert message back to BB Code for editing a post
        $txt = preg_replace( "#(<table border =(.+?) bgcolor =(.+?) bordercolor =(.+?) cellpadding =(.+?) cellspacing =(.+?)>)(.+?)(</table>)#is",
        "[table bd =\\2 bgc =\\3 bdc =\\4 cp =\\5 cs =\\6\]\\7\[/table\]", $txt );

        If you have an Invision this will work and you can have it but you also need to write a Javascript that will create the prompts that presents the info so that it will produce the properly spaced and in the proper order tag or it will not work, you can not put the attributes in any order you want they have to be as shown above and with the spacing as shown, if you wrote bd then bdg then bdg the preg_replace would fail, that is what was happening with my script.

          I see what you are trying to do now. Since you are using invision board you should try getting support on their site.

            It is fixed and working on two web sites plus my local machine, IPB is not going to support a third parties software in this case mine. I port MOD from phpBB2 to PHP-Nuke so that they will work, I also modify the IPB forums mostly 1.2 and 1.3 to see what I mean go to one of them and see what I am talking about the two IPB Nuke sites are http://portedmods.astahost.com/ipb/ and http://dhost.info/stevedemarcus/ipb/ and I have 3 regular PHP-Nuke sites that are heavily modded by me with downloads available of all Ported MODs on the main site NukeXtra
            also a PC-Nuke test site that I am modding for another Pack release of NukeExtra at Nuke Optimisers & NukeXtra Visiting any of these sites and viewing any of the forums you will see that I have mods that you will not see on a Standard PHP-Nuke without the MODs that I write, and the same for the Invision board that I modified for the IPB-Nuke as well as standalone IPB boards. Go to one of the Test forums on any of the above sites and try a post to see a menu kind of similar to these on a VBulletin that is used here, you will notice that not noly do I have the list icon but also an ordered list icon plus 5 icons just for creating tables, and icons for code, HTML, PHP, and SQL entry with syntax highlighting, and many other features not seen hardly anywhere mostly because I write this stuff myself.

              Write a Reply...