Keep line breaks with htmlentities?

In the form I create, I have textareas that will contain a nice amount of text with line breaks enabled, for example:

Sentence One

Offer 2

I submit this to my form using htmlentities , ex:

 . htmlentities($_POST['textarea']) . 

This, however, results in the following in a letter ( HTML letter ), which is sent in form:

Offer one offer two

Is there a way to keep line breaks using htmlentities ?

+6
source share
2 answers

Use nl2br() , which converts \n to <br /> :

 . nl2br(htmlentities($_POST['textarea'])) . 
+18
source

No, It is Immpossible. If you need a line break in an HTML document, you should use either the display: block element (and friends), or the line break element ( <br> ), or the white-space: pre element.

You can generate <br> elements using PHP using the nl2br function.

+1
source

Source: https://habr.com/ru/post/916100/


All Articles