skip to Main Content

I’ve made a site where the user can type somthing in a textarea. The text is then saved to a databse I have at phpMyAdmin. However, when I retrive the data from the database, and ‘echo’ it on the screen, all the line breaks the user have included, is gone.

Does anyone have a solution for this?

I’ve tried with different things like including a button for ‘new line’ in the UI, but that’s not very convenient. I wonder if there’s any other way of displaying the line breaks?

2

Answers


  1. You might want to use the nl2br() function:

    $text = "Hello,
    world!";
    echo nl2br($text);
    

    This will echo:

    Hello,<br>
    world!
    
    Login or Signup to reply.
  2. If you want to display the text exactly as it was entered on a web page, perhaps a <pre></pre> block may work best for you.

    It would be best if you did more to sanitize any user-provided text you intend on returning to a browser, though, to avoid XSS and other exploits.

    For example, what if a user sent you a script block, and you returned that to any one’s browser?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search