Accept <br> for new line but filter html tags in php
I want my input or textarea value only accept <br>
for having new lines
$comment = trim($comment);
$comment = stripslashes($comment) ;
Although I do wa开发者_如何学运维nt to accept <br>
for new lines but I dont want to accept html tags
any suggestion ?
The better solution is to allow users to enter new lines in text areas, and translate them to <br>
tags when you display the text with nl2br
.
When you display the text, first encode html entities, then translate the newlines to <br>
tags. If you use nl2br
before htmlspecialchars
, you'll wind up encoding the <br>
tags as well.
echo (nl2br(htmlspecialchars($text));
You can allow the text area to take newline characters. Then you can use those as <br>
s, just replace them after trimming and stripping all tags, or use a tool to display plain text as html.
You shouldn't be calling stripslashes, unless you're in the middle of building an SQL query.
you can use strip_tags
which takes $allowable_tags
as its second parameter:
$comment = strip_tags($comment, '<br>');
http://www.php.net/manual/en/function.strip-tags.php
精彩评论