研究链接:
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>
没有特定的黑名单,但有一个特定的白名单。
答案 0 :(得分:2)
此功能可能对您有所帮助,但未经过高度测试。除了您指定的标签外,它将对所有标签执行重要操作
function html_entity_decode_matches($matches){
return html_entity_decode($matches[0]);
}
function htmlentities_exclude($string, $exclude_array){
$string = htmlentities($string); //htmlentities all
$ent_sl = ">"; //>
if (is_array($exclude_array) AND !empty($exclude_array)){
foreach($exclude_array as $exc){
$exc = str_replace(array("<", ">"), "", $exc);
$ent = str_replace("/", "\/", htmlentities("<{$exc}"));
$ent_e = str_replace("/", "\/", htmlentities("</{$exc}>"));
//do decode on <tag...>
$string = preg_replace_callback("/{$ent}(.*?){$ent_sl}/", "html_entity_decode_matches", $string);
//do decode on <\tag>
$string = preg_replace_callback("/{$ent_e}/", "html_entity_decode_matches", $string);
}
}
return $string;
}
echo htmlentities_exclude('<b><script>alert("something");</script></b>', array("<b>"));
Output:
<b><script>alert("something");</script></b>
答案 1 :(得分:1)
您可以使用PHP DOM对象来实现此目的,首先创建一个元素(在您的情况下它是&lt; b&gt;)并提供编码字符串作为其主体(内部HTML),如下所示,
<?php
define('CHARSET', 'UTF-8');
define('REPLACE_FLAGS', ENT_HTML5);
function htmlcleaned($string) {
return str_replace(array("<", ">"), array("<", ">"), $string);
}
$dom = new DOMDocument('1.0', 'utf-8');
$element = $dom->createElement('b', htmlcleaned('<script>alert("something");</script>'));
$dom->appendChild($element);
$html = $dom->saveXML();
echo $html;
?>
您可以使用内置函数而不是创建这样的函数,
<?php
define('CHARSET', 'UTF-8');
define('REPLACE_FLAGS', ENT_HTML5);
$dom = new DOMDocument('1.0', 'utf-8');
$element = $dom->createElement('b', htmlspecialchars('<script>alert("something");</script>', ENT_NOQUOTES));
$dom->appendChild($element);
$html = $dom->saveXML();
echo $html;
?>