SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    california
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Preserving input line breaks

    I have a textarea in which a user inputs data. It it put in a text column in a database. When I retrieve the data, however, I can't get any of the line break (returns) in the data to show. If I wrap it in <pre></pre>, the line breaks are there, but it also has a weird padding and font. And the data in the database, when viewed through phpmyadmin, has all the appropriate line breaks.

    Any ideas on how I can get the line breaks without <pre></pre>?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    HTML ignores whitespace. If you hit enter 1,000 times when writing an HTML page, only a single space appears on the page. Your PHP is outputting HTML to the browser, and the results are no different. The line breaks are there, but pressing the enter key on the keyboard isn't how you produce a visible break in HTML.

    You need to replace the line breaks with <br /> tags. There's a helpful function which does this: nl2br().

    PHP Code:
    echo nl2br($textvar); 


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
  •