我正在尝试使用PHP将HTML转换为实体,但我需要排除和标记。
下面是我的代码示例
<?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");
?>发布于 2012-05-01 19:54:00
让我建议你使用BBCode,它会更安全。
发布于 2012-05-01 19:35:36
编辑:
好吧,我已经想出了一个办法。
使用这个函数而不是前面的函数:
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;
}现在使用上面的字符串对其进行了测试。
https://stackoverflow.com/questions/10396844
复制相似问题