Hey all,

I have a form that stores it's content in a database. When the stored information contains the html of a table it seems to cause display problems when displaying the contents using nl2br.

So when information is stored in the database like this...

Power Savings mail-in rebates...<table width="50%"  border="0" cellpadding="3">

... get's displayed in a web page using this...

<?php echo nl2br($row_rs_promo['promo_details']); ?>

... the code in the page when displayed in a browser looks like this...

Power Savings mail-in rebates...<br>

<br>
    <br>
    <br>
    <br>
  <br>
    <br>
    <br>
    <br>
    <br>

<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

<table border="0" cellpadding="3" width="50%">

I'm not sure where nl2br is finding so many breaks, or even one for that matter since the code in the database is on one line "rebates...<table".

Any ideas?

Thanks a lot as always!
Peter

    since your storing the html anyway, just don't use nl2br()

      Hey dagon,

      I wish it were that easy. The vast majority of form submissions are plain text and need nl2br for formatting. HTML is only inserted occasionally by the user and as in this case can follow several paragraphs of plain text.

        then i assume the users are adding the line breaks

        you will need to clean up their input

        $in= preg_replace("/(\r\n)+/","\n\n",$out);

          I'm not sure if there are normal line breaks even there. I think it has something to do with creating extra line breaks because of the presence of table tags.

          This is what's in the database...

          mail-in rebates...<table width="50%"

          Are there really a bunch of line breaks between the "..." and "<table"? I tried deleting this portion of text and rewriting it so as to remove and line breaks but the problem still persists.

          I tried using a function instead of nl2br and it didn't really help much...

          function nl2br2($text)
          {
              return preg_replace("/\r\n|\n|\r/", "<br>", $text);
          }
          echo nl2br2($row_rs_promo['promo_details']); 

            try removing\replacing chr(10) and chr(13). Thats the same as \n \r but may give a different result. You can use ord() to see what theses characters 'actully' are.

              Thanks for your help so far, dagon.

              I ran a test and this might help explain what is happening...

              I typed this into a blank form and saved it to the database...

              Some text.
              
              <table>
              <tr><td>Content of table.</td></tr>
              </table>

              When displaying this in the browser and looking at the page's code I see this...

              Some text.<br><br><br><br><table>...........

              Notice the 4 <br> tags even though I only hit the enter key twice.

              Now if I add a few more table rows like this...

              Some text.
              
              <table>
              <tr><td>Content of table.</td></tr>
              <tr><td>Content of table.</td></tr>
              <tr><td>Content of table.</td></tr>
              <tr><td>Content of table.</td></tr>
              <tr><td>Content of table.</td></tr>
              <tr><td>Content of table.</td></tr>
              </table>

              The page's source code now becomes this...

              Some text.<br><br><br><br><br><br><br><br><br><table>...........

              Notice there are now 9 <br> tags instead of 4. For some reason more <br> tags are created when the number of form elements increase. This would mean a complex table can have a HUGE gap between it and the paragraph of text before it.

              I think maybe form tags are being seen at line breaks somehow....??

                OHHHHhhh... I think I get what it's doing.

                It sees a line break after each row of table elements which would be correct if this information were going to be displayed as text only as it would need to keep its formatting. But because the table is executed as html when displayed, all these <br> tags are forced above the table for some reason. Hmmmm, how to handle this....

                  sounds like your form is doing the adding of line breaks

                    Confirmed...

                    If I place all the table elements on a single line as below, no extra line breaks are generated...

                    Some text.
                    
                    <table><tr><td>Content of table.</td></tr><tr><td>Content of table.</td></tr><tr><td>Content of table.</td></tr><tr><td>Content of table.</td></tr><tr><td>Content of table.</td></tr><tr><td>Content of table.</td></tr></table> 
                    
                    
                    

                    Now to think of the best way to handle these mash-ups of plain text and html....

                      Yes I agree. The form is adding line breaks which is necessary for formatting with plain text records. The problem is that it's also trying to format the html code.

                      Oh well, at least I know what's causing the problem. I'll have to give some thought on how to get around this... other than adding unformatted html as one big blob to avoid line breaks.

                      Thanks again for your help!

                        Thanks dagon. I think it is time to update the app with an html editor as you suggested. And thanks for the suggestion of fckeditor, never heard of that one before. Looks good.

                          Write a Reply...