Hello,

I am writing a script to take the contents of a textarea and store in a DB.

The data from the textarea
My example is

This is a line
This is a new line

I then send the $_POST variable through a sanitiser function and then through the nbbc.php parser.

The problem is the linebreaks. I know they are there as when you view the field in phpMyAdmin the data is show as

This is a line
This is a new line

however what I actually want to store in the DB is

This is a new line<br>This is a new line

I have tried nl2br but this does not work as this is used to display (echo) the data with line breaks.

I have tried many things found on this forum but none seem to help. Cany anyone help me please to convert the linebreaks directly into <br> so the can store in the DB

    But why store the <br>'s if that isn't in the original data? Couldn't you store the data in its original form and simply use [man]nl2br/man when displaying the data from the DB?

      Pigmaster wrote:

      I have tried nl2br but this does not work as this is used to display (echo) the data with line breaks.

      Actually, echo is use to display (echo) the data. nl2br doesn't display anything.

      But bradgrafelman's got the right idea: don't contaminate your data with stuff that would only need to be stripped out again if you decide to use the data for something different.

        Weedpacket;10907046 wrote:

        Actually, echo is use to display (echo) the data. nl2br doesn't display anything.

        But bradgrafelman's got the right idea: don't contaminate your data with stuff that would only need to be stripped out again if you decide to use the data for something different.

        nl2br is used to display (i shall not use the word echo but I shall use output) to the screen, you can not use nl2br to assign the data to a variable eg $example = nl2br($data); This does not work, so how can I get the data with <br> linebreaks into a variable to use with the templating system.

        I am using XTemplate

          bradgrafelman;10907011 wrote:

          But why store the <br>'s if that isn't in the original data? Couldn't you store the data in its original form and simply use [man]nl2br/man when displaying the data from the DB?

          As I said in my first post, I am parsering the textarea through the nbbc.php parser into html.

          Currently when running the textarea though the parser the linebreaks are there but invisible. I would like to convert them to <br> and them store.

          So the the data is being modified from BBcode to html, all works fine except the line breaks. ie they are not there in the HTML output from the parser.

            Pigmaster wrote:

            you can not use nl2br to assign the data to a variable eg $example = nl2br($data); This does not work,

            While I could just cite the manual entry for [man]nl2br[/man], I spent twenty seconds writing and running these:

            <?php
            $data = "Text with
            linebreaks";
            
            $example = nl2br($data);
            ?>

            I get no output from this.

            <?php
            $data = "Text with
            linebreaks";
            
            $example = nl2br($data);
            echo strlen($example);
            ?>

            Now I get "27", as I should.

            Works for me.

              Weedpacket;10907196 wrote:

              While I could just cite the manual entry for [man]nl2br[/man], I spent twenty seconds writing and running these:

              <?php
              $data = "Text with
              linebreaks";
              
              $example = nl2br($data);
              ?>

              I get no output from this.

              <?php
              $data = "Text with
              linebreaks";
              
              $example = nl2br($data);
              echo strlen($example);
              ?>

              Now I get "27", as I should.

              Works for me.

              Thanks for the reply, but you do not seem to understand my question that I am trying to put data from an Textarea into a variable and convert the line breaks to <br> then store it in the DB.

              I need the <br> in the database.

                bradgrafelman has already suggested not doing that, but leaving the data pristine instead.

                But if you do want to add linebreak HTML to the text before inserting it in the database, [man]nl2br[/man] will do the job.

                  For example, it'd be a simple matter of:

                  <?php
                  
                  $query = "SELECT data FROM bar WHERE foobar='news'";
                  $exec = mysql_query($query);
                  $news = mysql_result($exec, 0, 'data');
                  
                  // Since "data" is in raw format, but we're outputting it into the HTML
                  // and we expect line breaks to become <br> at this point, use nl2br
                  
                  echo "Here's the latest news:<hr>$news";

                  Otherwise, storing the <br>'s in the database destroys data integrity, meaning if you ever wanted to put this data back into a textarea (say for editing what had been submitted before), you'd have to do another transform to clean up the data (namely removing the <br>'s in exchange for line breaks). This effectively doubles the amount of work you do on the data versus only transforming the data when you need to.

                    Write a Reply...