本文目錄一覽:
用php過濾html部分標籤
$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標籤
$str=preg_replace(“/(\/?head.*?)/si”,””,$str); //過濾head標籤
$str=preg_replace(“/(\/?meta.*?)/si”,””,$str); //過濾meta標籤
$str=preg_replace(“/(\/?body.*?)/si”,””,$str); //過濾body標籤
$str=preg_replace(“/(\/?link.*?)/si”,””,$str); //過濾link標籤
$str=preg_replace(“/(\/?form.*?)/si”,””,$str); //過濾form標籤
$str=preg_replace(“/cookie/si”,”COOKIE”,$str); //過濾COOKIE標籤
$str=preg_replace(“/(applet.*?)(.*?)(\/applet.*?)/si”,””,$str); //過濾applet標籤
$str=preg_replace(“/(\/?applet.*?)/si”,””,$str); //過濾applet標籤
$str=preg_replace(“/(style.*?)(.*?)(\/style.*?)/si”,””,$str); //過濾style標籤
$str=preg_replace(“/(\/?style.*?)/si”,””,$str); //過濾style標籤
$str=preg_replace(“/(title.*?)(.*?)(\/title.*?)/si”,””,$str); //過濾title標籤
$str=preg_replace(“/(\/?title.*?)/si”,””,$str); //過濾title標籤
$str=preg_replace(“/(object.*?)(.*?)(\/object.*?)/si”,””,$str); //過濾object標籤
$str=preg_replace(“/(\/?objec.*?)/si”,””,$str); //過濾object標籤
$str=preg_replace(“/(noframes.*?)(.*?)(\/noframes.*?)/si”,””,$str); //過濾noframes標籤
$str=preg_replace(“/(\/?noframes.*?)/si”,””,$str); //過濾noframes標籤
$str=preg_replace(“/(i?frame.*?)(.*?)(\/i?frame.*?)/si”,””,$str); //過濾frame標籤
$str=preg_replace(“/(\/?i?frame.*?)/si”,””,$str); //過濾frame標籤
$str=preg_replace(“/(script.*?)(.*?)(\/script.*?)/si”,””,$str); //過濾script標籤
$str=preg_replace(“/(\/?script.*?)/si”,””,$str); //過濾script標籤
$str=preg_replace(“/javascript/si”,”Javascript”,$str); //過濾script標籤
$str=preg_replace(“/vbscript/si”,”Vbscript”,$str); //過濾script標籤
$str=preg_replace(“/on([a-z]+)\s*=/si”,”On\\1=”,$str); //過濾script標籤
$str=preg_replace(“//si”,”#”,$str); //過濾script標籤,如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標籤的正則表達式:
複製代碼 代碼如下:
/?[^]+
//過濾所有html標籤的屬性的正則表達式:
$html = preg_replace(“/([a-zA-Z]+)[^]*/”,”\\1″,$html);
3,過濾部分html標籤的正則表達式的排除式(比如排除p,即不過濾p):
複製代碼 代碼如下:
/?[^pP/]+
4,過濾部分html標籤的正則表達式的枚舉式(比如需要過濾apb等):
複製代碼 代碼如下:
/?[aApPbB][^]*
5,過濾部分html標籤的屬性的正則表達式的排除式(比如排除alt屬性,即不過濾alt屬性):
複製代碼 代碼如下:
\s(?!alt)[a-zA-Z]+=[^\s]*
6,過濾部分html標籤的屬性的正則表達式的枚舉式(比如alt屬性):
複製代碼 代碼如下:
(\s)alt=[^\s]*
如何解決php函數strip
在做微信推送的時候發現一個strip_tags()函數,對於不能過濾,完美的展示出了 ,之前都是直接打開瀏覽器,以為不顯示就認為是正確的。卻沒有仔細的查看源碼。
php:$str = ‘哈哈哈 呵呵’;echo strip_tags($str); html:哈哈哈 呵呵
從上可以看出strip_tags()函數並沒有把 給過濾掉,所以我們只能手動的使用str_replace()將 替換掉。
php:echo strip_tags(str_replace(‘ ‘,”,$str)); html:哈哈哈呵呵
很簡單的就解決了php函數strip_tags()過濾不了 的問題
關於這個問題,你如果不明白,問他們也可以問我也可以,我這些都是在後盾上學的,有空可以去看一下,就算不喜歡也沒關係啊,何樂而不為呢?
strip_tags — 從字元串中去除 HTML 和 PHP 標記?
這函數挺有用的,這是總結後的知識點,希望能幫到你!
strip_tags
(PHP 4, PHP 5, PHP 7, PHP 8)
strip_tags — 從字元串中去除 HTML 和 PHP 標記
說明
strip_tags ( string $str , string $allowable_tags = ? ) : string
該函數嘗試返回給定的字元串 str 去除空字元、HTML 和 PHP 標記後的結果。它使用與函數 fgetss() 一樣的機制去除標記。
參數
str
輸入字元串。
allowable_tags
使用可選的第二個參數指定不被去除的字元列表。
注意:
HTML 注釋和 PHP 標籤也會被去除。這裡是硬編碼處理的,所以無法通過 allowable_tags 參數進行改變。
注意:
In PHP 5.3.4 and later, self-closing XHTML tags are ignored and only non-self-closing tags should be used in allowable_tags. For example, to allow both
and , you should use:’);
?
返回值
返回處理後的字元串。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/156558.html