我正在尝试使用PHP将HTML转换为实体,但我需要除了< br>和< a>标签
这是我的代码
的一个例子 <?php
$string[0] = "<a href='http://hidd3n.tk'>Needs to stay</a> Filler text in between
<br><br> <script src='http://malicious.com/'></script> NEEDS to go";
$string[1] = htmlentities($string[0], ENT_QUOTES, "UTF-8");
?>
答案 0 :(得分:3)
我建议您使用BBCode,这样会更安全。
答案 1 :(得分:-3)
修改强>
好的,我已经找到了办法。
使用此功能比以前的功能更安全:
function convert_myhtml_entities($string){
$string = htmlentities($string, ENT_NOQUOTES, "UTF-8");
$string = preg_replace('/<\s*br\s*(\/|)\s*>/U','<br$1>',$string);
$string = preg_replace('/<\s*a(.*)\s*>/U','<a$1>',$string);
$string = preg_replace('/<\s*\/\s*a\s*>/U','</a>',$string);
return $string;
}
现在用上面的字符串进行测试。