欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP實(shí)現(xiàn)過(guò)濾各種HTML標(biāo)簽

 更新時(shí)間:2015年05月17日 16:33:11   投稿:hebedich  
在做項(xiàng)目的過(guò)程中,我們經(jīng)常需要用到過(guò)濾一些html標(biāo)簽來(lái)實(shí)現(xiàn)提高數(shù)據(jù)的安全性,其實(shí)就是刪除那些對(duì)應(yīng)用程序有潛在危害的數(shù)據(jù)。它用于去除標(biāo)簽以及刪除或編碼不需要的字符。

首先分享一些比較常見(jiàn)的

$str=preg_replace("/<s*imgs+[^>]*?srcs*=s*('|")(.*?)\1[^>]*?/?s*>/i","", $str); //過(guò)濾img標(biāo)簽

$str=preg_replace("/s+/","", $str); //過(guò)濾多余回車(chē)

$str=preg_replace("/<[ ]+/si","<",$str); //過(guò)濾<__("<"號(hào)后面帶空格)

$str=preg_replace("/<!--.*?-->/si","",$str); //注釋

$str=preg_replace("/<(!.*?)>/si","",$str); //過(guò)濾DOCTYPE

$str=preg_replace("/<(/?html.*?)>/si","",$str); //過(guò)濾html標(biāo)簽

$str=preg_replace("/<(/?head.*?)>/si","",$str); //過(guò)濾head標(biāo)簽

$str=preg_replace("/<(/?meta.*?)>/si","",$str); //過(guò)濾meta標(biāo)簽

$str=preg_replace("/<(/?body.*?)>/si","",$str); //過(guò)濾body標(biāo)簽

$str=preg_replace("/<(/?link.*?)>/si","",$str); //過(guò)濾link標(biāo)簽

$str=preg_replace("/<(/?form.*?)>/si","",$str); //過(guò)濾form標(biāo)簽

$str=preg_replace("/cookie/si","COOKIE",$str); //過(guò)濾COOKIE標(biāo)簽

$str=preg_replace("/<(applet.*?)>(.*?)<(/applet.*?)>/si","",$str); //過(guò)濾applet標(biāo)簽

$str=preg_replace("/<(/?applet.*?)>/si","",$str); //過(guò)濾applet標(biāo)簽

$str=preg_replace("/<(style.*?)>(.*?)<(/style.*?)>/si","",$str); //過(guò)濾style標(biāo)簽

$str=preg_replace("/<(/?style.*?)>/si","",$str); //過(guò)濾style標(biāo)簽

$str=preg_replace("/<(title.*?)>(.*?)<(/title.*?)>/si","",$str); //過(guò)濾title標(biāo)簽

$str=preg_replace("/<(/?title.*?)>/si","",$str); //過(guò)濾title標(biāo)簽

$str=preg_replace("/<(object.*?)>(.*?)<(/object.*?)>/si","",$str); //過(guò)濾object標(biāo)簽

$str=preg_replace("/<(/?objec.*?)>/si","",$str); //過(guò)濾object標(biāo)簽

$str=preg_replace("/<(noframes.*?)>(.*?)<(/noframes.*?)>/si","",$str); //過(guò)濾noframes標(biāo)簽

$str=preg_replace("/<(/?noframes.*?)>/si","",$str); //過(guò)濾noframes標(biāo)簽

$str=preg_replace("/<(i?frame.*?)>(.*?)<(/i?frame.*?)>/si","",$str); //過(guò)濾frame標(biāo)簽

$str=preg_replace("/<(/?i?frame.*?)>/si","",$str); //過(guò)濾frame標(biāo)簽

$str=preg_replace("/<(script.*?)>(.*?)<(/script.*?)>/si","",$str); //過(guò)濾script標(biāo)簽

$str=preg_replace("/<(/?script.*?)>/si","",$str); //過(guò)濾script標(biāo)簽

$str=preg_replace("/javascript/si","Javascript",$str); //過(guò)濾script標(biāo)簽

$str=preg_replace("/vbscript/si","Vbscript",$str); //過(guò)濾script標(biāo)簽

$str=preg_replace("/on([a-z]+)s*=/si","On\1=",$str); //過(guò)濾script標(biāo)簽

$str=preg_replace("/&#/si","&#",$str); //過(guò)濾script標(biāo)簽

更簡(jiǎn)單些的寫(xiě)法:

function delhtml($str){  //清除html標(biāo)簽
$st=-1; //開(kāi)始
$et=-1; //結(jié)束
$stmp=array();
$stmp[]="&nbsp;";
$len=strlen($str);
for($i=0;$i<$len;$i++){
  $ss=substr($str,$i,1);
  if(ord($ss)==60){ //ord("<")==60
  $st=$i;
  }
  if(ord($ss)==62){ //ord(">")==62
  $et=$i;
  if($st!=-1){
   $stmp[]=substr($str,$st,$et-$st+1);
  }
  }
}
$str=str_replace($stmp,"",$str);
return $str;
}

再來(lái)一個(gè):

function clear_html_label($html) 
{ 
$search = array ("'<script[^>]*?>.*?</script>'si", "'<[/!]*?[^<>]*?>'si", "'([rn])[s]+'", "'&(quot|#34);'i", "'&(amp|#38);'i", "'&(lt|#60);'i", "'&(gt|#62);'i", "'&(nbsp|#160);'i", "'&(iexcl|#161);'i", "'&(cent|#162);'i", "'&(pound|#163);'i", "'&(copy|#169);'i", "'&#(d+);'e"); 
$replace = array ("", "", "1", """, "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), "chr(1)");
return preg_replace($search, $replace, $html); 
}

以上三種方法均可以實(shí)現(xiàn),不過(guò)各有優(yōu)劣,小伙伴們根據(jù)自己的項(xiàng)目需求來(lái)選擇吧。

相關(guān)文章

最新評(píng)論