Ninoo
Ninoo

Reputation: 25

How to make <br> working even there is htmlspecialchars function

Hi is there any way to make <br> working even there is htmlspecialchars function. My problem is when i put a text inside the function htmlspecialchars there be no new lines even the text has new lines. My code is like:

$text_val = "
this is a line
it is a new line
one more line
";

$final_text = str_replace("\n","<br>",$text_val);

$text = htmlspecialchars($final_text);
echo $text;

and result is like:

this is a line it is a new line one more line

But i want it like the first:

this is a line
it is a new line
one more line

thank you guys.

Upvotes: 2

Views: 145

Answers (2)

Kamran
Kamran

Reputation: 531

Use built in PHP function nl2br $final_text = nl2br($text_val);

Upvotes: 6

AndrewL64
AndrewL64

Reputation: 16301

You're looking for the htmlspecialchars_decode() function but using the htmlspecialchars() function instead.

Replace this line:

$text = htmlspecialchars($final_text);

with this:

$text = htmlspecialchars_decode($final_text);

Upvotes: 1

Related Questions