我正在尝试创建一个函数,该函数将删除任何潜在的脚本标记,但不会删除其他标记,如p、li、ol、span、h1。。。
这就是我到目前为止所拥有的。我还写了<和>作为编码字符"%3C"answers"%3E"以及HTML名称和编号。尝试为第一个执行正则表达式,如您所见"^<(/)?script>$"。但它不起作用:D
function smartFilter($string) {
$string = strtolower($string);
if (strpos($string, "<script>") !== FALSE || strpos($string, "<script>") !== FALSE || strpos($string, "<script>") !== FALSE || strpos($string, "%3Cscript%3E") !== FALSE) {
$unallowed = array("^<('/)?script>$", "<script>", "</script>", "%3Cscript%3E", "%3C/script%3E", "<script>", "<script>");
return preg_replace($unallowed, "", $string);
} else {
return $string;
}
}
为什么不使用php中的strip_tags
?此处链接。
这完成了我要求的所有工作:D
function smartFilter($string) {
return strip_tags($string, "<p><li><ol><ul><h1><h2><h3><h4><h5><span><b><u><i>");
}