php常用过滤非法/特殊字符串的方法
2013-08-06 13:42:42 来源:WEB开发网'document.cookie' => '',
'document.write' => '',
'window.location' => '',
"javascript\s*:" => '',
"Redirect\s+302" => '',
'<!--' => '<!--',
'-->' => '-->'
);
foreach ($bad as $key=>$val){
$html=preg_replace("#".$key."#i",$val,$html);
}
return $html;
}
//过滤html标签以及敏感字符
function cleanHtml($html){
return cleanYellow(htmlspecialchars($html));
}
//过滤部分HTML标签
function cleanFilter($html){
$html=trim($html);
$html=preg_replace("/<p[^>]*?>/is","<p>",$html);
$html=preg_replace("/<div[^>]*?>/is","<div>",$html);
$html=preg_replace("/<ul[^>]*?>/is","<ul>",$html);
$html=preg_replace("/<li[^>]*?>/is","<li>",$html);
$html=preg_replace("/<span[^>]*?/is","<span>",$html);
$html=preg_replace("/<a[^>]*?>(.*)?<\/a>/is","\${1}",$html);
$html=preg_replace("/<table[^>]*?>/is","<table>",$html);
$html=preg_replace("/<tr[^>]*?>/is","<tr>",$html);
$html=preg_replace("/<td[^>]*?>/is","<td>",$html);
$html=preg_replace("/<ol[^>]*?>/is","<ol>",$html);
$html=preg_replace("/<form[^>]*?>/is","",$html);
$html=preg_replace("/<input[^>]*?>/is","",$html);
return $html;
}
//过滤非法的敏感字符串
function cleanYellow($txt){
$txt=str_replace(
array("黄色","性爱","做爱","我日","我草","我靠","尻","共产党","胡锦涛","毛泽东",
"政府","中央","研究生考试","性生活","色情","情色","我考","麻痹","妈的","阴道",
"淫","奸","阴部","爱液","阴液","臀","色诱","煞笔","傻比","阴茎","法轮功","性交","阴毛","江泽民"),
array("*1*","*2*","*3*","*4*","*5*","*6*","*7*","*8*","*9*","*10*",
"*11*","*12*","*13*","*14*","*15*","*16*","*17*","*18*","*19*","*20*",
"*21*","*22*","*23*","*24*","*25*","*26*","*27*","*28*","*29*","*30*","*31*","*32*","*33*","*34*"),
$txt);
return $txt;
赞助商链接