SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question replacing newlines with <br> when - your opinions?

    When I take the input from a 'textarea' box in a form I was wondering when to strip newlines and replace with html line breaks?

    Would it be best to run a replace on the string as soon as it has been input and store in the DB with the br's or to store 'as is' and to replace on retrieval an display.

    Obviously there is a small overhead to running on every display, but are there any reasons to?

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would do them on display as you will have the data stored in the exact way it was posted. Then you can do anything you want with it. For example, if you wanted to make your website more usable and provide one version with normal spacing and one with double line spacing then you can just alter your function, you don't have to alter the data in the database

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  3. #3
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah good, that was the way I was leaning anyway.

  4. #4
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for that Sean. Solved the riddle of "What is the point of the nl2br() function" - had been storing it in the database then getting ever increasing <br>'s

  5. #5
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you are better off using:

    preg_replace("/(\015\012)|(\015)|(\012)/","<br>", $text )

    than nl2br() as it will work for all platforms.

  6. #6
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nl2br() should replace linebreaks for all operating systems

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  7. #7
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in my testing, nl2br() only works on \n newlines, not \r (e.g. Mac). so that i can still use nl2br() anyway, since it's much faster than any manual replacement you can do, i convert all newlines to \n before inserting in the DB:

    PHP Code:
    $txt preg_replace('/\r\n|\r/'"\n"$txt); 
    simple. then nl2br() will work fine when displaying the text.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  8. #8
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you reported it as a bug Matt?

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  9. #9
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no, b/c i imagine that that's how they designed it.


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
  •