Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to user nl2br function with htmlspecialchars in phpmysql

Tags:

php

How to insert nl2br function with htmlspecialchars? I have a site where input is taken from textarea and nl2br is used to convert next line to a paragraph. When I tried with htmlspecialchars I got the below output. Here I wrote three 'test' words in textarea and saved in database. I am using htmlspecialchars to prevent html injections but because of this function nl2br function is not working. Can you tell be how to work around this problem?

test<br/>test<br/>test<br/>
like image 335
test Avatar asked Aug 02 '11 10:08

test


3 Answers

yo do:

htmlspecialchars(nl2br($text));

you need:

nl2br(htmlspecialchars($text));
like image 193
RiaD Avatar answered Oct 22 '22 15:10

RiaD


Call nl2br after you call htmlspecialchars:

echo nl2br(htmlspecialchars($the_text));
like image 26
Delan Azabani Avatar answered Oct 22 '22 17:10

Delan Azabani


It's about using the right order,

htmlspecialchars(nl2br($string)); will produce the result you describe. nl2br(htmlspecialchars($string)); will produce the result you wish.

like image 2
Johan Avatar answered Oct 22 '22 17:10

Johan