SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Wizard bronze trophy JRMillion's Avatar
    Join Date
    Apr 2004
    Location
    Arlington VA
    Posts
    2,094
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    maintain line breaks when storing txt from textarea

    hello, quick question.

    if someone types something like
    Code:
    Hello
    World
    into my textarea, how can I prevent it from becoming
    Code:
    HelloWorld
    when I retreive it from the DB?

    Do I have to replace line breaks with <br>'s before I store it?
    what would be the best way to do that?
    James Rice :: Ex-Mentor
    www.jamesrice.net

  2. #2
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey

    check out http://www.php.net/nl2br

    I think that should help!

    - Mark

  3. #3
    SitePoint Enthusiast MstrBob's Avatar
    Join Date
    Dec 2004
    Location
    New York City
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, for the linebreaks to be perserved in an (X)HTML document, you will need to replace newline characters with <br> (or you can keep the newline characters, your choice). Often, though, I find that users most often go to a new line at the end of a paragraph, so I prefer wrapping text in <p> tags.

    The simple way is with the nl2br() Keep in mind, though, that this will output the <br /> tag. So, if you are using HTML, this is not desirable. So, one can use:

    str_replace(chr(13), '<br>', $text)

    Or, if you want to wrap the text in <p> tags, which might be a bit more semantic:

    PHP Code:
    $text='<p>'.$text.'</p>';
    $text=str_replace(chr(13).chr(10), "</p>\n<p>"$text);
    $text=str_replace(chr(10).'<p></p>'''$text); 

  4. #4
    SitePoint Wizard bronze trophy JRMillion's Avatar
    Join Date
    Apr 2004
    Location
    Arlington VA
    Posts
    2,094
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks guys, its working
    James Rice :: Ex-Mentor
    www.jamesrice.net

  5. #5
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great stuff!

    - Mark

  6. #6
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MstrBob
    [...] Keep in mind, though, that this will output the <br /> tag. So, if you are using HTML, this is not desirable. [...]
    There is no disadvantage whatsoever when using <br /> instead of <br>. The former is just the XHTML compliant way of writing <br>. It makes no practical difference.

  7. #7
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Icheb
    There is no disadvantage whatsoever when using <br /> instead of <br>. The former is just the XHTML compliant way of writing <br>. It makes no practical difference.
    It actually works out better using <br /> In my opinion!


  8. #8
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works better with what? Validation? It would be nice of you to provide an explanation.

  9. #9
    SitePoint Enthusiast MstrBob's Avatar
    Join Date
    Dec 2004
    Location
    New York City
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Icheb
    There is no disadvantage whatsoever when using <br /> instead of <br>. The former is just the XHTML compliant way of writing <br>. It makes no practical difference.
    Yes, there actually is. If one is using HTML, than why would you want <br />? It's not valid HTML. It's a different syntax. There is no closing br tag, and that method of closing tags isn't valid HTML syntax. If you are writing an XHTML page, then, yes it's fine. But for HTML, it's invalid. Ergo, a simply str_replace will add correct character.

  10. #10
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To quote myself:
    Quote Originally Posted by Icheb
    It makes no practical difference
    With an emphasis on practical.

  11. #11
    SitePoint Enthusiast MstrBob's Avatar
    Join Date
    Dec 2004
    Location
    New York City
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To someone concerned about users being able to access content no matter the user agent, it does make a difference, as it may cause rendering issues. Of course, if you see no practical benefit in writing proper HTML documents, we might as well continue down that path so that browsers can get more bloated at attempting to guess at what we want. Why is it that improper HTML is ignored? If one were to write invalid script, it would fail. Because IE will guess, it's okay to write bad HTML?

  12. #12
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You know, I have no idea if you had such a rough holiday or why do you have to vent on something that's pointless? I was talking about the practical differences between <br> and <br />, not validation in its entirety.
    It's always good to read before you answer.

  13. #13
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Icheb
    It works better with what? Validation? It would be nice of you to provide an explanation.
    I was just saying valid xhtml is the way to go.

    - Mark

  14. #14
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mark_W
    I was just saying valid xhtml is the way to go.

    - Mark
    Yep.

    Users can add a comment on my own site. The process page treats their text like so:

    PHP Code:
    $comment nl2br(addslashes(htmlspecialchars($_POST['comment']))); 
    So, when put into the db:

    Blah blah blah blah blah blah blah<br />
    blah blah blah blah blah blah blah blah blah blah<br />

    Then, to display the comments I simply use:

    PHP Code:
    $comment stripslashes($article_comments['comment']);

    echo 
    "<p>$comment</p>"
    Keeps it marked up properly and works fine. Dunno if this post really contributes to the thread but meh ...

  15. #15
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gregor002
    Dunno if this post really contributes to the thread but meh ...
    Every bit of info helps!


  16. #16
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you need stripslashes() to display the comment properly, you probably have magic_quotes_gpc enabled and you add the slashes twice when using addslashes().

  17. #17
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Icheb
    If you need stripslashes() to display the comment properly, you probably have magic_quotes_gpc enabled and you add the slashes twice when using addslashes().

    I'm sure my host has this set to off, that's why I use addslashes() for adding any user input to the db and stripslashes() for writing to the browser.

  18. #18
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you check the entry in the database with, for an example, phpMyAdmin, you'll see that there won't be any slashes then.

  19. #19
    SitePoint Enthusiast MstrBob's Avatar
    Join Date
    Dec 2004
    Location
    New York City
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Icheb
    You know, I have no idea if you had such a rough holiday or why do you have to vent on something that's pointless? I was talking about the practical differences between <br> and <br />, not validation in its entirety.
    It's always good to read before you answer.
    Exactly, you said there's no practical difference between the two. I pointed out Valid code as being an important, and practical difference. You did not see valid code as being practical, hence my reply.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •