我正在寻找用于书名的文本区域的白名单。我想允许的唯一字符是字母数字、空格、连字符、下划线、句点和 <br>
标签。理想情况下,任何其他特殊字符都应转换为它们的 htmlentities。如果有帮助,该页面使用 php、html、javascript 和 jquery。有人有什么想法吗??
文本区域输入示例:
<textarea>
I have this book called Sample- a Fake Book.
</textarea>
最佳答案
如果这与安全性有任何关系,即确保数据始终安全显示,则必须在服务器端完成。
除了 <br>
标记,只需 HTML 编码即可完成您想要的操作。
最好的方法可能是使用 htmlentities然后带上 <br>
的背面:
$encoded_text = htmlentities($input_text);
// replace the encoded <br>'s with the original <br>'s
$final_text = str_replace(htmlentities("<br>"), "<br>", $encoded_text);
尝试获得此行为并仍然使用 htmlentities
的另一种方法是取代<br>
带有占位符的标签,贯穿 htmlentities
,然后将其替换回去。类似的东西:
$br_placeholder = "XX_BR_PLACEHOLDER_XX";
$text_with_placeholders = str_replace("<br>", $br_placeholder, $input_text);
$text_with_htmlentities = htmlentities($text_with_placeholders);
$final_text = str_replace($br_placeholder, "<br>", $text_with_htmlentities);
关于php - html文本区域的白名单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7122884/