Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve line breaks with htmlentities?

Tags:

php

In a form that I'm building, I have textareas which will include a nice amount of text, with line breaks included, such as:

Sentence one

Sentence two

I am posting this to my form by using htmlentities, ex:

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

This however, produces the following in the email (HTML email) that gets sent by the form:

Sentence one Sentence two

Is there anyway to preserve line breaks with htmlentities?

like image 230
Charlie Avatar asked May 18 '12 14:05

Charlie


2 Answers

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

. nl2br(htmlentities($_POST['textarea'])) .
like image 188
ThiefMaster Avatar answered Oct 02 '22 22:10

ThiefMaster


No, it isn't possible. If you want a line break in an HTML document then you have to use either a display: block (and friends) element, a line break element (<br>) or a white-space: pre element.

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

like image 28
Quentin Avatar answered Oct 02 '22 20:10

Quentin