Using nl2br to preserve textarea new lines to mysq

2020-05-02 05:44发布

问题:

I have a form with a textarea whose results are inserted into a mysql database. I'm using nl2br to preserve the line breaks.

However, because this inserts br's in the text, when a user goes to edit what they've entered in the textarea, it shows all the br's in the textarea which were saved in mysql (looks ugly for people who don't know html).

So, if I don't use nl2br, the line breaks look nice when echoed back in the textarea but not saved in the database correctly. If I use nl2br, the line breaks are saved in the database correctly but look ugly when echoed back. How do I echo the saved textarea contents back onto the page without showing the br's all over the place?

回答1:

Store the test in the database with break lines and when you echo it out to the screen run it through nl2br.