【发布时间】:2016-05-09 08:09:02
【问题描述】:
研究链接:
How do you apply htmlentities selectively? 和 PHP function to strip tags, except a list of whitelisted tags and attributes
它们很接近,但并不像预期的那样。
我尝试了什么?
<?php
define('CHARSET', 'UTF-8');
define('REPLACE_FLAGS', ENT_HTML5);
function htmlcleaned($string) {
$string = htmlentities($string);
return str_replace(
array("<i>", "<b>", "</i>", "</b>", "<p>", "</p>"),
array("<i>", "<b>", "</i>", "</b>", "<p>", "</p>"), $string);
}
echo htmlcleaned("<p>How are you?</p><p><b>This is bold</b></p><p><i>This is italic</i></p><p><u>This is underline</u></p><p><br></p><ul><li>This is list item 1</li><li>This is list item 2</li></ul><p><br></p><ol><li>This is ordered list item 1</li><li>This is ordered list item 2</li></ol><p><a target='_blank' style='color: #1c5c76;' href='http://www.google.com'>http://www.google.com</a></p><p>This is plain text again.<br></p><script>alert('attempt csrf');</script><p><p>This is P tag example</p></p>");
?>
我想达到什么目标?
如果输入是:
<b><script>alert("something");</script></b>
那么输出将是:
<b><script&rt;("something");</script$rt;</b>
没有具体的黑名单,但有具体的白名单。
【问题讨论】:
-
如果第二个链接包含您可以尝试调整的解决方案,您出于什么原因列出了“研究链接”?
-
@MarcinOrlowski 再读一遍我的问题,你可能知道:)
-
@deceze 谢谢。我可能会寻找 DOMDocument...
标签: php regex html-entities strip-tags