Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

BBCode --> HTML table

Last post 05-15-2011, 8:03 PM by Aussie Susan. 1 replies.
Sort Posts: Previous Next
  •  05-13-2011, 2:02 PM 82055

    BBCode --> HTML table

    Hello

    I have a forum that allows custom bulletin board code to be executed through PHP files. Now I'm trying to construct a html table tag from some kind of 'template'. A pipe symbol designates the delimiter between individual cells of the table and a linebreak designates that a new table row should be inserted. Ofcourse, there is no way to determine how much cells and rows a user will insert into the table, so I need a working regex to find and replace every occurance of the '|' symbol and every linebreak within the table bbcode.

    Pipe symbol (|) should become: </td><td>
    Linebreak (\n) should become: </td></tr><tr><td>

     So far, I have this code which puts the 'container' around the text, but I have no idea how I would go about replacing the actual content. I'm new to regex writing.
    In this code, {$_tag} = table. It is in a foreach loop, because there can be multiple bbcode tags which do the same thing.

    $txt = preg_replace( "/\[{$_tag}\](.+?)\[\/{$_tag}\]/is", "<table class=\"bbcode_table\"><tr class=\"first_row\"><td>\\1</td></tr></table>", $txt );

     

    Filed under: , , ,
  •  05-15-2011, 8:03 PM 82109 in reply to 82055

    Re: BBCode --> HTML table

    The "simplistic" answers are to use the following pattern and replacement strings:

    Pattern: \|    and replacement string "</td><td>"

    Pattern: \n   and replacement string "</td></tr><tr><td>"

    Now, if you apply these two patterns and replacement strings (separately) to your source text, it will replace EVERY pipe character and EVERY newline with the whole file with the associated text.

    However I do note you mentioned "...within the table bbcode". Now, I have absolutely no knowledge of bbcode and so I have no idea how to detect if a pipe or newline are inside or outside of a bbcode table. perhaps is it better if you show is a suitable snippet of the original source text (including parts that are inside and outside a bbcode table that include pip and newline characters) and also how you want it to look after it has been processed.

    Susan

View as RSS news feed in XML