How to handle line breaks when storing textarea entry in a database?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan

    How to handle line breaks when storing textarea entry in a database?

    I want to save textarea contents to a mysql database with the
    paragraph breaks intact without having to type paragraph or break tags
    in HTML. How can I do that. So far, although it occurs naturally when
    I save the contents to a file, it doesn't break up the paragraphs
    properly when I save it to my database.

    Thanks!

    Jonathan

  • steve

    #2
    Re: How to handle line breaks when storing textarea entry in a d

    "Jonathan" wrote:[color=blue]
    > I want to save textarea contents to a mysql database with the
    > paragraph breaks intact without having to type paragraph or break[/color]
    tags[color=blue]
    > in HTML. How can I do that. So far, although it occurs naturally[/color]
    when[color=blue]
    > I save the contents to a file, it doesn’t break up the
    > paragraphs
    > properly when I save it to my database.
    >
    > Thanks!
    >
    > Jonathan
    > http://thewebdevelopment.com[/color]

    Jonathan, textarea uses cr\lf to break lines. That is how it would
    be saved into db, if your program does no extra translations. Upon
    display on html page, cr\lf could then be replaced by html break
    tags.

    I don’t know of paragraph breaks. I believe that is simply double
    cr\lf’s.

    --
    http://www.dbForumz.com/ This article was posted by author's request
    Articles individually checked for conformance to usenet standards
    Topic URL: http://www.dbForumz.com/mySQL-handle...ict131179.html
    Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=438213

    Comment

    • sdfgsd

      #3
      Re: How to handle line breaks when storing textarea entry in a database?


      "Jonathan" <thewebdevelopm ent@yahoo.com> wrote in message
      news:6769f8ab.0 407192357.39e27 35e@posting.goo gle.com...[color=blue]
      > I want to save textarea contents to a mysql database with the
      > paragraph breaks intact without having to type paragraph or break tags
      > in HTML. How can I do that. So far, although it occurs naturally when
      > I save the contents to a file, it doesn't break up the paragraphs
      > properly when I save it to my database.[/color]


      You need to replace your \r\n with <p />
      and your \n with <br />

      php code sample (handles dos and *nix text):

      $string = ereg_replace("( \r\n\r\n|\n\n|\ r\r)", "<p />", $_POST['testtext']);

      ^^^^^^^^^^^^^

      Text from query

      $string = stripcslashes(e reg_replace("(\ r\n|\n|\r)", "<br />", $string));

      print $string;


      Comment

      • steve

        #4
        Re: How to handle line breaks when storing textarea entry in a d

        "Jonathan" wrote:[color=blue]
        > I want to save textarea contents to a mysql database with the
        > paragraph breaks intact without having to type paragraph or break[/color]
        tags[color=blue]
        > in HTML. How can I do that. So far, although it occurs naturally[/color]
        when[color=blue]
        > I save the contents to a file, it doesn’t break up the
        > paragraphs
        > properly when I save it to my database.
        >
        > Thanks!
        >
        > Jonathan
        > http://thewebdevelopment.com[/color]

        Jonathan, textarea uses cr\lf to break lines. That is how it would
        be saved into db, if your program does no extra translations. Upon
        display on html page, cr\lf could then be replaced by html break
        tags.

        I don’t know of paragraph breaks. I believe that is simply double
        cr\lf’s.

        --
        http://www.dbForumz.com/ This article was posted by author's request
        Articles individually checked for conformance to usenet standards
        Topic URL: http://www.dbForumz.com/mySQL-handle...ict131179.html
        Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=438213

        Comment

        • sdfgsd

          #5
          Re: How to handle line breaks when storing textarea entry in a database?


          "Jonathan" <thewebdevelopm ent@yahoo.com> wrote in message
          news:6769f8ab.0 407192357.39e27 35e@posting.goo gle.com...[color=blue]
          > I want to save textarea contents to a mysql database with the
          > paragraph breaks intact without having to type paragraph or break tags
          > in HTML. How can I do that. So far, although it occurs naturally when
          > I save the contents to a file, it doesn't break up the paragraphs
          > properly when I save it to my database.[/color]


          You need to replace your \r\n with <p />
          and your \n with <br />

          php code sample (handles dos and *nix text):

          $string = ereg_replace("( \r\n\r\n|\n\n|\ r\r)", "<p />", $_POST['testtext']);

          ^^^^^^^^^^^^^

          Text from query

          $string = stripcslashes(e reg_replace("(\ r\n|\n|\r)", "<br />", $string));

          print $string;


          Comment

          • 2metre

            #6
            Re: How to handle line breaks when storing textarea entry in a d

            steve wrote:
            [color=blue]
            > "Jonathan" wrote:[color=green]
            > > I want to save textarea contents to a mysql database with the
            > > paragraph breaks intact without having to type paragraph or break[/color]
            > tags[color=green]
            > > in HTML. How can I do that. So far, although it occurs naturally[/color]
            > when[color=green]
            > > I save the contents to a file, it doesn’t break up the
            > > paragraphs
            > > properly when I save it to my database.
            > >
            > > Thanks!
            > >
            > > Jonathan
            > > http://thewebdevelopment.com[/color]
            >
            > Jonathan, textarea uses cr\lf to break lines. That is how it would
            > be saved into db, if your program does no extra translations. Upon
            > display on html page, cr\lf could then be replaced by html break
            > tags.
            >
            > I don’t know of paragraph breaks. I believe that is simply double
            > cr\lf’s.
            >[/color]
            I would like to suggest a small correction:

            A 'paragraph break' is actually CR/LF. A textarea will break lines using
            wordwrap. (To some people a paragraph break is known as a 'hard break':
            its hard-coded, whereas a wordwrap is a 'soft break': software
            calculates it.) A wordwrap doesn't actually use any symbol wthin the
            stored text, since the position of the line breaks will alter depending
            on the media in which the text is displayed.

            HTML also uses wordwrap, but it ignores CR/LF. In its place you can use
            either a <BR> for linebreak or <p> for paragraphs. If you have a block
            of text which uses CR/LFs that you wish to display in HTML, then you'll
            need to substitute <br> for each of the CR/LFs. (in PHP there is a
            function nl2br() that does this.)

            History: CR/LF goes back to the days of early printers where the codes
            replicated the actions of a typewriter. CR (carriage return) returned
            the carriage to the start of a new line. LF (line feed) advanced to the
            next line. The lever at the left end of a typewriter combined moving
            the carriage and advancing the line. Typists used to apply a CR/LF at
            the end of every line and an extra CR/LF between paragraphs. As electric
            typewriters came into use they would automatically apply the CR/LF when
            the end of the line was reached.

            Now with wordprocessing and wordwrap, the CR/LF at the end of each line
            is no longer neccessary.

            Any corrections/improvements to this explanation would be greatfully
            received.

            Mike

            Comment

            • 2metre

              #7
              Re: How to handle line breaks when storing textarea entry in a d

              steve wrote:
              [color=blue]
              > "Jonathan" wrote:[color=green]
              > > I want to save textarea contents to a mysql database with the
              > > paragraph breaks intact without having to type paragraph or break[/color]
              > tags[color=green]
              > > in HTML. How can I do that. So far, although it occurs naturally[/color]
              > when[color=green]
              > > I save the contents to a file, it doesn’t break up the
              > > paragraphs
              > > properly when I save it to my database.
              > >
              > > Thanks!
              > >
              > > Jonathan
              > > http://thewebdevelopment.com[/color]
              >
              > Jonathan, textarea uses cr\lf to break lines. That is how it would
              > be saved into db, if your program does no extra translations. Upon
              > display on html page, cr\lf could then be replaced by html break
              > tags.
              >
              > I don’t know of paragraph breaks. I believe that is simply double
              > cr\lf’s.
              >[/color]
              I would like to suggest a small correction:

              A 'paragraph break' is actually CR/LF. A textarea will break lines using
              wordwrap. (To some people a paragraph break is known as a 'hard break':
              its hard-coded, whereas a wordwrap is a 'soft break': software
              calculates it.) A wordwrap doesn't actually use any symbol wthin the
              stored text, since the position of the line breaks will alter depending
              on the media in which the text is displayed.

              HTML also uses wordwrap, but it ignores CR/LF. In its place you can use
              either a <BR> for linebreak or <p> for paragraphs. If you have a block
              of text which uses CR/LFs that you wish to display in HTML, then you'll
              need to substitute <br> for each of the CR/LFs. (in PHP there is a
              function nl2br() that does this.)

              History: CR/LF goes back to the days of early printers where the codes
              replicated the actions of a typewriter. CR (carriage return) returned
              the carriage to the start of a new line. LF (line feed) advanced to the
              next line. The lever at the left end of a typewriter combined moving
              the carriage and advancing the line. Typists used to apply a CR/LF at
              the end of every line and an extra CR/LF between paragraphs. As electric
              typewriters came into use they would automatically apply the CR/LF when
              the end of the line was reached.

              Now with wordprocessing and wordwrap, the CR/LF at the end of each line
              is no longer neccessary.

              Any corrections/improvements to this explanation would be greatfully
              received.

              Mike

              Comment

              Working...