简体   繁体   中英

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 want 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.

除非您正在构建SQL查询,否则不要调用反斜杠。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM