php完美過濾HTML代碼的函數

來源:互聯網
上載者:User

例子,使用strip_tags()函數過濾所有html

 代碼如下 複製代碼

$str = '<a href="#">href</a>';
echo htmlspecialchars($str);
echo strip_tags($str);

輸出結果為

&lt;a href=&quot;#&quot;&gt;href&lt;/a&gt;

href


上面函數有一個問題就是包括html標籤,img標籤都過濾掉了,如果我們希望保留圖片怎麼辦

在網上找到一個函數

 代碼如下 複製代碼

function uh($str)
 {
     $farr = array(
         "/s+/",                                                                   
                        //過濾多餘的空白
         "/<(/?)(script|i?frame|style|html|body|title|link|meta|?|%)([^>]*?)>/isu",
   //過濾 <script 等可能引入惡意內容或惡意改變顯示布局的代碼,如果不需要插入flash等,還可
 以加入<object的過濾
         "/(<[^>]*)on[a-za-z]+s*=([^>]*>)/isu",                                    
 //過濾網頁特效的on事件
     
    );
    $tarr = array(
         " ",
         "<123>",           //如果要直接清除不安全的標籤,這裡可以留空
         "12",
    );
  $str = preg_replace( $farr,$tarr,$str);
    return $str;
 }

這樣就可以過濾指定標籤了,上面方法還不會我們可參考下面辦法

 

 代碼如下 複製代碼
 <br>$str=preg_replace("/\s+/", " ", $str); //過濾多餘斷行符號 <br>$str=preg_replace("/&lt;[ ]+/si","&lt;",$str); //過濾&lt;__("&lt;"號後面帶空格) <br><br>$str=preg_replace("/&lt;\!--.*?--&gt;/si","",$str); //注釋 <br>$str=preg_replace("/&lt;(\!.*?)&gt;/si","",$str); //過濾DOCTYPE <br>$str=preg_replace("/&lt;(\/?html.*?)&gt;/si","",$str); //過濾html標籤 <br>$str=preg_replace("/&lt;(\/?head.*?)&gt;/si","",$str); //過濾head標籤 <br>$str=preg_replace("/&lt;(\/?meta.*?)&gt;/si","",$str); //過濾meta標籤 <br>$str=preg_replace("/&lt;(\/?body.*?)&gt;/si","",$str); //過濾body標籤 <br>$str=preg_replace("/&lt;(\/?link.*?)&gt;/si","",$str); //過濾link標籤 <br>$str=preg_replace("/&lt;(\/?form.*?)&gt;/si","",$str); //過濾form標籤 <br>$str=preg_replace("/cookie/si","COOKIE",$str); //過濾COOKIE標籤 <br><br>$str=preg_replace("/&lt;(applet.*?)&gt;(.*?)&lt;(\/applet.*?)&gt;/si","",$str); //過濾applet標籤 <br>$str=preg_replace("/&lt;(\/?applet.*?)&gt;/si","",$str); //過濾applet標籤 <br><br>$str=preg_replace("/&lt;(style.*?)&gt;(.*?)&lt;(\/style.*?)&gt;/si","",$str); //過濾style標籤 <br>$str=preg_replace("/&lt;(\/?style.*?)&gt;/si","",$str); //過濾style標籤 <br><br>$str=preg_replace("/&lt;(title.*?)&gt;(.*?)&lt;(\/title.*?)&gt;/si","",$str); //過濾title標籤 <br>$str=preg_replace("/&lt;(\/?title.*?)&gt;/si","",$str); //過濾title標籤 <br><br>$str=preg_replace("/&lt;(object.*?)&gt;(.*?)&lt;(\/object.*?)&gt;/si","",$str); //過濾object標籤 <br>$str=preg_replace("/&lt;(\/?objec.*?)&gt;/si","",$str); //過濾object標籤 <br><br>$str=preg_replace("/&lt;(noframes.*?)&gt;(.*?)&lt;(\/noframes.*?)&gt;/si","",$str); //過濾noframes標籤 <br>$str=preg_replace("/&lt;(\/?noframes.*?)&gt;/si","",$str); //過濾noframes標籤 <br><br>$str=preg_replace("/&lt;(i?frame.*?)&gt;(.*?)&lt;(\/i?frame.*?)&gt;/si","",$str); //過濾frame標籤 <br>$str=preg_replace("/&lt;(\/?i?frame.*?)&gt;/si","",$str); //過濾frame標籤 <br><br>$str=preg_replace("/&lt;(script.*?)&gt;(.*?)&lt;(\/script.*?)&gt;/si","",$str); //過濾script標籤 <br>$str=preg_replace("/&lt;(\/?script.*?)&gt;/si","",$str); //過濾script標籤 <br>$str=preg_replace("/javascript/si","Javascript",$str); //過濾script標籤 <br>$str=preg_replace("/vbscript/si","Vbscript",$str); //過濾script標籤 <br>$str=preg_replace("/on([a-z]+)\s*=/si","On\\1=",$str); //過濾script標籤 <br>$str=preg_replace("/&amp;#/si","&amp;#",$str); //過濾script標籤,如javAsCript:alert( <br>


如果只要過濾過濾html標籤,js代碼,css樣式標籤

 代碼如下 複製代碼


<?php
$str = preg_replace( "@<script(.*?)</script>@is", "", $str );
$str = preg_replace( "@<iframe(.*?)</iframe>@is", "", $str );
$str = preg_replace( "@<style(.*?)</style>@is", "", $str );
$str = preg_replace( "@<(.*?)>@is", "", $str );
?>

這樣即可了哦。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.