PHP實現(xiàn)過濾各種HTML標(biāo)簽
更新時間:2015年05月17日 16:33:11 投稿:hebedich
在做項目的過程中,我們經(jīng)常需要用到過濾一些html標(biāo)簽來實現(xiàn)提高數(shù)據(jù)的安全性,其實就是刪除那些對應(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); //過濾<__("<"號后面帶空格)
$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)簽
更簡單些的寫法:
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;
}
再來一個:
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);
}
以上三種方法均可以實現(xiàn),不過各有優(yōu)劣,小伙伴們根據(jù)自己的項目需求來選擇吧。
您可能感興趣的文章:
- php截取html字符串及自動補全html標(biāo)簽的方法
- php使HTML標(biāo)簽自動補全閉合函數(shù)代碼
- PHP實現(xiàn)HTML標(biāo)簽自動補全代碼
- PHP正則表達式過濾html標(biāo)簽屬性(DEMO)
- php過濾表單提交的html等危險代碼
- php過濾HTML標(biāo)簽、屬性等正則表達式匯總
- 淺析php過濾html字符串,防止SQL注入的方法
- php正則過濾html標(biāo)簽、空格、換行符的代碼(附說明)
- php 正則 過濾html 的超鏈接
- php完全過濾HTML,JS,CSS等標(biāo)簽
- PHP實現(xiàn)網(wǎng)頁內(nèi)容html標(biāo)簽補全和過濾的方法小結(jié)【2種方法】
相關(guān)文章
PHP實現(xiàn)根據(jù)圖片色界在不同位置加水印的方法
這篇文章主要介紹了PHP實現(xiàn)根據(jù)圖片色界在不同位置加水印的方法,涉及php使用MagickWand模塊操作圖片添加水印的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-08-08
apache和php之間協(xié)同工作的配置經(jīng)驗分享
閑暇之時也會對PHP知識做一些研究與大家共享,首先工欲善其事,必先利其器,可能比喻不是很恰當(dāng),php也需要一系列的環(huán)境配置,以下是我配置php的一些手稿,經(jīng)測試已成功運行了2013-04-04
基于PHP安裝zip拓展,以及l(fā)ibzip安裝的問題
下面小編就為大家分享一篇基于PHP安裝zip拓展,以及l(fā)ibzip安裝的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
PHP中使用foreach和引用導(dǎo)致程序BUG的問題介紹
PHP 引用有些類似 C 語言指針, 但一些重要的特性和 C 語言指針不一樣, 如果不注意, 會導(dǎo)致程序 BUG. foreach 操作的是數(shù)組或?qū)ο蟮目截? 但 PHP5, 可以使用引用操作對象元素本身2012-09-09

