detecting new lines in textarea's using php

Posted on 2007-10-02
Last Modified: 2013-12-13
I'm sure this has been asked before but I haven't been able to find a solution.  Basically, a user fills out a textarea and they can enter breaks by pressing the Enter key.  How do I use str_replace() to detect the newline characters and replace them with a <br/> so that when it goes into and gets pulled out of the db it will be displayed correctly by the browser?
Question by:jpve
    LVL 1

    Accepted Solution

    nl2br() function does that.

    you could also use str_replace("\n","<br>",$string)
    LVL 3

    Assisted Solution

    I am usually using this function to change new lines to <br />: nl2br (more information on

    $stringwithbreaks = nl2br($textareastring);


    Author Comment

    Thanks a ton guys.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    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.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    737 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

    21 Experts available now in Live!

    Get 1:1 Help Now