国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP實現過濾各種HTML標簽

2024-05-04 23:35:20
字體:
來源:轉載
供稿:網友

在做項目的過程中,我們經常需要用到過濾一些html標簽來實現提高數據的安全性,其實就是刪除那些對應用程序有潛在危害的數據。它用于去除標簽以及刪除或編碼不需要的字符。

首先分享一些比較常見的

 

 
  1. $str=preg_replace("/<s*imgs+[^>]*?srcs*=s*('|")(.*?)/1[^>]*?/?s*>/i","", $str); //過濾img標簽 
  2.  
  3. $str=preg_replace("/s+/",""$str); //過濾多余回車 
  4.  
  5. $str=preg_replace("/<[ ]+/si","<",$str); //過濾<__("<"號后面帶空格) 
  6.  
  7. $str=preg_replace("/<!--.*?-->/si","",$str); //注釋 
  8.  
  9. $str=preg_replace("/<(!.*?)>/si","",$str); //過濾DOCTYPE 
  10.  
  11. $str=preg_replace("/<(/?html.*?)>/si","",$str); //過濾html標簽 
  12.  
  13. $str=preg_replace("/<(/?head.*?)>/si","",$str); //過濾head標簽 
  14.  
  15. $str=preg_replace("/<(/?meta.*?)>/si","",$str); //過濾meta標簽 
  16.  
  17. $str=preg_replace("/<(/?body.*?)>/si","",$str); //過濾body標簽 
  18.  
  19. $str=preg_replace("/<(/?link.*?)>/si","",$str); //過濾link標簽 
  20.  
  21. $str=preg_replace("/<(/?form.*?)>/si","",$str); //過濾form標簽 
  22.  
  23. $str=preg_replace("/cookie/si","COOKIE",$str); //過濾COOKIE標簽 
  24.  
  25. $str=preg_replace("/<(applet.*?)>(.*?)<(/applet.*?)>/si","",$str); //過濾applet標簽 
  26.  
  27. $str=preg_replace("/<(/?applet.*?)>/si","",$str); //過濾applet標簽 
  28.  
  29. $str=preg_replace("/<(style.*?)>(.*?)<(/style.*?)>/si","",$str); //過濾style標簽 
  30.  
  31. $str=preg_replace("/<(/?style.*?)>/si","",$str); //過濾style標簽 
  32.  
  33. $str=preg_replace("/<(title.*?)>(.*?)<(/title.*?)>/si","",$str); //過濾title標簽 
  34.  
  35. $str=preg_replace("/<(/?title.*?)>/si","",$str); //過濾title標簽 
  36.  
  37. $str=preg_replace("/<(object.*?)>(.*?)<(/object.*?)>/si","",$str); //過濾object標簽 
  38.  
  39. $str=preg_replace("/<(/?objec.*?)>/si","",$str); //過濾object標簽 
  40.  
  41. $str=preg_replace("/<(noframes.*?)>(.*?)<(/noframes.*?)>/si","",$str); //過濾noframes標簽 
  42.  
  43. $str=preg_replace("/<(/?noframes.*?)>/si","",$str); //過濾noframes標簽 
  44.  
  45. $str=preg_replace("/<(i?frame.*?)>(.*?)<(/i?frame.*?)>/si","",$str); //過濾frame標簽 
  46.  
  47. $str=preg_replace("/<(/?i?frame.*?)>/si","",$str); //過濾frame標簽 
  48.  
  49. $str=preg_replace("/<(script.*?)>(.*?)<(/script.*?)>/si","",$str); //過濾script標簽 
  50.  
  51. $str=preg_replace("/<(/?script.*?)>/si","",$str); //過濾script標簽 
  52.  
  53. $str=preg_replace("/javascript/si","Javascript",$str); //過濾script標簽 
  54.  
  55. $str=preg_replace("/vbscript/si","Vbscript",$str); //過濾script標簽 
  56.  
  57. $str=preg_replace("/on([a-z]+)s*=/si","On/1=",$str); //過濾script標簽 
  58.  
  59. $str=preg_replace("/&#/si","&#",$str); //過濾script標簽 

更簡單些的寫法:

 

 
  1. function delhtml($str){ //清除html標簽 
  2. $st=-1; //開始 
  3. $et=-1; //結束 
  4. $stmp=array(); 
  5. $stmp[]=""
  6. $len=strlen($str); 
  7. for($i=0;$i<$len;$i++){ 
  8. $ss=substr($str,$i,1); 
  9. if(ord($ss)==60){ //ord("<")==60 
  10. $st=$i
  11. if(ord($ss)==62){ //ord(">")==62 
  12. $et=$i
  13. if($st!=-1){ 
  14. $stmp[]=substr($str,$st,$et-$st+1); 
  15. $str=str_replace($stmp,"",$str); 
  16. return $str

再來一個:

 

 
  1. function clear_html_label($html)  
  2. {  
  3. $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");  
  4. $replace = array ("""""1"""", "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), "chr(1)"); 
  5. return preg_replace($search$replace$html);  

以上三種方法均可以實現,不過各有優劣,小伙伴們根據自己的項目需求來選擇吧。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大余县| 宁津县| 淅川县| 东光县| 桐城市| 三河市| 上饶县| 股票| 荆门市| 建湖县| 浠水县| 咸阳市| 库车县| 武川县| 宽甸| 大悟县| 上杭县| 西乌珠穆沁旗| 宜兴市| 樟树市| 华亭县| 玉树县| 鄂州市| 四子王旗| 澎湖县| 黄浦区| 福贡县| 龙井市| 芜湖市| 五台县| 济宁市| 东辽县| 凤山市| 平安县| 定边县| 龙泉市| 清水县| 鄂温| 高尔夫| 扎鲁特旗| 高密市|