PHP: How to deal with line break in text area?

Posted on 2007-07-27
Last Modified: 2013-12-12
I have a text area, but when user type stuff and then hit line break, and write stuff, when I get the data from $_POST and then store in db, and then retrieve from db and print on web page, the content just become one paragrah without any line break.

Any idea how to solve that?
Question by:jtcy
    LVL 54

    Accepted Solution

    First check to see if the line breaks are in the data in your database.  If so you just need to use nl2br() to convert the data from the database for the webpage.  The new line characters will be replaced with the <br> tag.  If it is hard to look at your data in the database then view the html source of the page.  If you see the line breaks in the source then the nl2br() function will do it.

    If you need help storing the new lines in the database then let me know and I can help with that.  Let me know if you have a question or need more info.

    LVL 49

    Assisted Solution

    In addition to b0lsc0tt, note that you need to do nl2br after your htmlspecialchars:

    echo nl2br(htmlspecialchars($contents))


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    The viewer will learn how to dynamically set the form action using jQuery.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now