PHP實(shí)現(xiàn)過濾各種HTML標(biāo)簽
更新時(shí)間:2015年05月17日 16:33:11 投稿:hebedich
在做項(xiàng)目的過程中,我們經(jīng)常需要用到過濾一些html標(biāo)簽來實(shí)現(xiàn)提高數(shù)據(jù)的安全性,其實(shí)就是刪除那些對(duì)應(yīng)用程序有潛在危害的數(shù)據(jù)。它用于去除標(biāo)簽以及刪除或編碼不需要的字符。
首先分享一些比較常見的
$str=preg_replace("/<s*imgs+[^>]*?srcs*=s*('|")(.*?)\1[^>]*?/?s*>/i","", $str); //過濾img標(biāo)簽 $str=preg_replace("/s+/","", $str); //過濾多余回車 $str=preg_replace("/<[ ]+/si","<",$str); //過濾<__("<"號(hào)后面帶空格) $str=preg_replace("/<!--.*?-->/si","",$str); //注釋 $str=preg_replace("/<(!.*?)>/si","",$str); //過濾DOCTYPE $str=preg_replace("/<(/?html.*?)>/si","",$str); //過濾html標(biāo)簽 $str=preg_replace("/<(/?head.*?)>/si","",$str); //過濾head標(biāo)簽 $str=preg_replace("/<(/?meta.*?)>/si","",$str); //過濾meta標(biāo)簽 $str=preg_replace("/<(/?body.*?)>/si","",$str); //過濾body標(biāo)簽 $str=preg_replace("/<(/?link.*?)>/si","",$str); //過濾link標(biāo)簽 $str=preg_replace("/<(/?form.*?)>/si","",$str); //過濾form標(biāo)簽 $str=preg_replace("/cookie/si","COOKIE",$str); //過濾COOKIE標(biāo)簽 $str=preg_replace("/<(applet.*?)>(.*?)<(/applet.*?)>/si","",$str); //過濾applet標(biāo)簽 $str=preg_replace("/<(/?applet.*?)>/si","",$str); //過濾applet標(biāo)簽 $str=preg_replace("/<(style.*?)>(.*?)<(/style.*?)>/si","",$str); //過濾style標(biāo)簽 $str=preg_replace("/<(/?style.*?)>/si","",$str); //過濾style標(biāo)簽 $str=preg_replace("/<(title.*?)>(.*?)<(/title.*?)>/si","",$str); //過濾title標(biāo)簽 $str=preg_replace("/<(/?title.*?)>/si","",$str); //過濾title標(biāo)簽 $str=preg_replace("/<(object.*?)>(.*?)<(/object.*?)>/si","",$str); //過濾object標(biāo)簽 $str=preg_replace("/<(/?objec.*?)>/si","",$str); //過濾object標(biāo)簽 $str=preg_replace("/<(noframes.*?)>(.*?)<(/noframes.*?)>/si","",$str); //過濾noframes標(biāo)簽 $str=preg_replace("/<(/?noframes.*?)>/si","",$str); //過濾noframes標(biāo)簽 $str=preg_replace("/<(i?frame.*?)>(.*?)<(/i?frame.*?)>/si","",$str); //過濾frame標(biāo)簽 $str=preg_replace("/<(/?i?frame.*?)>/si","",$str); //過濾frame標(biāo)簽 $str=preg_replace("/<(script.*?)>(.*?)<(/script.*?)>/si","",$str); //過濾script標(biāo)簽 $str=preg_replace("/<(/?script.*?)>/si","",$str); //過濾script標(biāo)簽 $str=preg_replace("/javascript/si","Javascript",$str); //過濾script標(biāo)簽 $str=preg_replace("/vbscript/si","Vbscript",$str); //過濾script標(biāo)簽 $str=preg_replace("/on([a-z]+)s*=/si","On\1=",$str); //過濾script標(biāo)簽 $str=preg_replace("/&#/si","&#",$str); //過濾script標(biāo)簽
更簡(jiǎn)單些的寫法:
function delhtml($str){ //清除html標(biāo)簽 $st=-1; //開始 $et=-1; //結(jié)束 $stmp=array(); $stmp[]=" "; $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; }
再來一個(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),不過各有優(yōu)劣,小伙伴們根據(jù)自己的項(xiàng)目需求來選擇吧。
您可能感興趣的文章:
- php截取html字符串及自動(dòng)補(bǔ)全html標(biāo)簽的方法
- php使HTML標(biāo)簽自動(dòng)補(bǔ)全閉合函數(shù)代碼
- PHP實(shí)現(xiàn)HTML標(biāo)簽自動(dòng)補(bǔ)全代碼
- PHP正則表達(dá)式過濾html標(biāo)簽屬性(DEMO)
- php過濾表單提交的html等危險(xiǎn)代碼
- php過濾HTML標(biāo)簽、屬性等正則表達(dá)式匯總
- 淺析php過濾html字符串,防止SQL注入的方法
- php正則過濾html標(biāo)簽、空格、換行符的代碼(附說明)
- php 正則 過濾html 的超鏈接
- php完全過濾HTML,JS,CSS等標(biāo)簽
- PHP實(shí)現(xiàn)網(wǎng)頁內(nèi)容html標(biāo)簽補(bǔ)全和過濾的方法小結(jié)【2種方法】
相關(guān)文章
PHP進(jìn)階學(xué)習(xí)之垃圾回收機(jī)制詳解
這篇文章主要介紹了PHP進(jìn)階學(xué)習(xí)之垃圾回收機(jī)制,詳細(xì)分析了php垃圾回收機(jī)制的概念、原理及對(duì)性能的影響,需要的朋友可以參考下2019-06-06PHP魔術(shù)方法之__call與__callStatic使用方法
這篇文章主要介紹了PHP魔術(shù)方法之__call與__callStatic方法,需要的朋友可以參考下2017-07-07PHP中使用glob函數(shù)實(shí)現(xiàn)一句話刪除某個(gè)目錄下的所有文件
這篇文章主要介紹了PHP中使用glob函數(shù)實(shí)現(xiàn)一句話刪除某個(gè)目錄下的所有文件,重點(diǎn)在glob函數(shù)的使用上,需要的朋友可以參考下2014-07-07