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[]=" "; $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)選擇吧。
您可能感興趣的文章:
- 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á)式過(guò)濾html標(biāo)簽屬性(DEMO)
- php過(guò)濾表單提交的html等危險(xiǎn)代碼
- php過(guò)濾HTML標(biāo)簽、屬性等正則表達(dá)式匯總
- 淺析php過(guò)濾html字符串,防止SQL注入的方法
- php正則過(guò)濾html標(biāo)簽、空格、換行符的代碼(附說(shuō)明)
- php 正則 過(guò)濾html 的超鏈接
- php完全過(guò)濾HTML,JS,CSS等標(biāo)簽
- PHP實(shí)現(xiàn)網(wǎng)頁(yè)內(nèi)容html標(biāo)簽補(bǔ)全和過(guò)濾的方法小結(jié)【2種方法】
相關(guān)文章
使用php判斷瀏覽器的類型和語(yǔ)言的函數(shù)代碼
用PHP判斷瀏覽器類型其實(shí)很簡(jiǎn)單。因?yàn)闉g覽器在和服務(wù)器連接時(shí)候都會(huì)先發(fā)送一些包含自己信息的內(nèi)容(瀏覽器類型、語(yǔ)言等)2013-02-02PHP進(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