php過濾HTML標(biāo)簽、屬性等正則表達(dá)式匯總
$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)簽,如javAsCript:alert(
清除空格,換行
function DeleteHtml($str)
{
$str = trim($str);
$str = strip_tags($str,"");
$str = ereg_replace("\t","",$str);
$str = ereg_replace("\r\n","",$str);
$str = ereg_replace("\r","",$str);
$str = ereg_replace("\n","",$str);
$str = ereg_replace(" "," ",$str);
return trim($str);
}
過濾HTML屬性
1,過濾所有html標(biāo)簽的正則表達(dá)式:
</?[^>]+>
//過濾所有html標(biāo)簽的屬性的正則表達(dá)式:
$html = preg_replace("/<([a-zA-Z]+)[^>]*>/","<\\1>",$html);
3,過濾部分html標(biāo)簽的正則表達(dá)式的排除式(比如排除<p>,即不過濾<p>):
</?[^pP/>]+>
4,過濾部分html標(biāo)簽的正則表達(dá)式的枚舉式(比如需要過濾<a><p><b>等):
</?[aApPbB][^>]*>
5,過濾部分html標(biāo)簽的屬性的正則表達(dá)式的排除式(比如排除alt屬性,即不過濾alt屬性):
\s(?!alt)[a-zA-Z]+=[^\s]*
6,過濾部分html標(biāo)簽的屬性的正則表達(dá)式的枚舉式(比如alt屬性):
(\s)alt=[^\s]*
PS:關(guān)于正則,這里再為大家推薦2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg
相關(guān)文章
讓CodeIgniter數(shù)據(jù)庫緩存自動過期的處理的方法
按官方的說法,緩存設(shè)置后永不過期,除非你調(diào)用方法主動刪除。這篇文章主要介紹了CodeIgniter數(shù)據(jù)庫緩存自動過期的處理,需要的朋友可以參考下2014-06-06
Yii使用find findAll查找出指定字段的實(shí)現(xiàn)方法
這篇文章主要介紹了Yii使用find findAll查找出指定字段的實(shí)現(xiàn)方法,非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09
PHP網(wǎng)頁游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(十四)
這篇文章主要介紹了PHP網(wǎng)頁游戲Xnova(ogame)源碼解讀的資源更新頁面部分,需要的朋友可以參考下2014-06-06
CI框架常用經(jīng)典操作類總結(jié)(路由,偽靜態(tài),分頁,session,驗(yàn)證碼等)
這篇文章主要介紹了CI框架常用經(jīng)典操作類,結(jié)合實(shí)例形式總結(jié)分析了CI框架URL、路由、偽靜態(tài)、分頁、session、驗(yàn)證碼等相關(guān)操作類與使用技巧,需要的朋友可以參考下2016-11-11
WordPress中制作導(dǎo)航菜單的PHP核心方法講解
這篇文章主要介紹了WordPress中制作導(dǎo)航菜單的PHP核心方法,即wp_get_nav_menu的相關(guān)參數(shù)的作用和用法,需要的朋友可以參考下2015-12-12
php實(shí)現(xiàn)數(shù)組篩選奇數(shù)和偶數(shù)示例
這篇文章主要介紹了php實(shí)現(xiàn)數(shù)組篩選奇數(shù)和偶數(shù)示例,需要的朋友可以參考下2014-04-04
yii使用activeFileField控件實(shí)現(xiàn)上傳文件與圖片的方法
這篇文章主要介紹了yii使用activeFileField控件實(shí)現(xiàn)上傳文件與圖片的方法,較為詳細(xì)的分析了activeFileField控件用于文件傳輸?shù)木唧w使用技巧,需要的朋友可以參考下2015-12-12

