導航:首頁 > 編程語言 > php去除html函數

php去除html函數

發布時間:2024-06-26 19:55:37

php 過濾掉html標簽及標簽內的所有內容

方法一:使用strip_tags()函數
strip_tags() 函數剝去字元串中的 HTML、XML 以及PHP的標簽。
使用案例:
$string = "<p>這里是潘旭博客</p>"
$newStr = strip_tags($string);
echo $newStr;

方法二:使用str_replace()函數
str_replace() 函數以其他字元替換字元串中的一些字元(區分大小寫)
使用案例:
$string = "<p>這里是潘旭博客</p>";
$newStr = str_replace(array("<p>","</p>"),array("",""));
echo $newStr;

另外還有一種是通過正則的方法,請參考:https://panxu.net/article/8385.html

② php屏蔽一段HTML代碼

<?php& `8 ^, }, E' j/ W' j

function StripHTML($string){. \, V/ t+ d8 o
$pattern=array ("'<script[^>]*?>.*?</script>'si", "'<style[^>]*?>.*?</style>'si", "'<[/!]*?[^<>]*?>'si", "'([
])[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", "'&(|#169);'i", "'&#(d+);'e");
$replace=array ("", "", "\1", "", "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), "chr(\1)");7 f: V6 a$ D! s: q
return preg_replace ($pattern, $replace, $str);
}
" e/ P+ M# ]0 O9 R0 q
?>
利用strip_tags()就可以把html標簽去除掉
<?php
$str = '<a href="http://harryxu.cn">harryxu</a>'; E' y! t( K6 m' |9 \
echo(htmlspecialchars($str) . '<br />');/ `" _* h" r# x5 i" \1 C
echo(strip_tags($str));
. P. t! i7 I5 D+ X% o
// output:
// <a href="http://harryxu.nc">harryxu</a> Y' T) V' y$ t- z: d% }! h
// harryxu
?>
去除所有js,html,css代碼

$search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 javascript
"'<style[^>]*?>.*?</style>'si", // 去掉 css
"'<[/!]*?[^<>]*?>'si", // 去掉 HTML 標記
"'<!--[/!]*?[^<>]*?>'si", // 去掉 注釋標記# m# g3 V9 F- U! `8 O4 l, [
"'([rn])[s]+'", // 去掉空白字元3 p$ c& X7 x- y7 Q
"'&(quot|#34);'i", // 替換 HTML 實體+ E. t- E, L% u# ?( W
"'&(amp|#38);'i",0 D; g# s' Z" v" L
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",# N* E! |7 }( y" p O1 O/ E
"'&(cent|#162);'i",2 T: J; m) a3 r
"'&(pound|#163);'i",8 E4 a: R4 B Z# V! \
"'&(|#169);'i",
"'&#(d+);'e"); // 作為 PHP 代碼運行

$replace = array ("",
"",
"",
"",1 `1 Z0 I' x3 n$ }1 Q' ?+ K
"1",! N) R x7 [/ ^6 E6 O3 l
""",
"&",
"<",. c2 t3 T! L+ l* p. d) x# Q# @
">",
" ",
chr(161),) i' y# E& k1 I
chr(162),( h3 q/ C3 X5 [: O7 ~9 z4 s0 ?
chr(163),' g+ P! A. A% M( R
chr(169),
"chr(1)");
//$document為需要處理字元串,如果來源為文件可以$document = file_get_contents($filename);
$out = preg_replace($search, $replace, $document);

③ php截取字元串以及去掉html標記

$str="帶html的字元串";
$str2=strip_tags($str);
echo $str2; //已經去掉html 標

④ 用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標簽的正則表達式的枚舉式(比如需要過濾<a><p><b>等):

復制代碼 代碼如下:

</?[aApPbB][^>]*>

5,過濾部分html標簽的屬性的正則表達式的排除式(比如排除alt屬性,即不過濾alt屬性):

復制代碼 代碼如下:

\s(?!alt)[a-zA-Z]+=[^\s]*

6,過濾部分html標簽的屬性的正則表達式的枚舉式(比如alt屬性):

復制代碼 代碼如下:

(\s)alt=[^\s]*

⑤ php 刪除mysql 資料庫html 代碼

樓上的方法就可以
<?php
echo strip_tags(資料庫中讀取出來的內容);

// 用此系統函數可以方便的去掉完整的html標簽 ,但如果其中html標簽有殘缺,殘缺的不能去掉
// 例如:strip_tags('<strong>你好 /strong>'); 會顯示: 你好 /strong>
?>

⑥ PHP如何去除HTML標簽

functioncut_tags_html($str)
{
$search=array("'<script[^>]*?>.*?</script>'si",//去掉javascript
"'<[/!]*?[^<>]*?>'si",//去掉HTML標記
"'([ ])[s]+'",//去掉空白字元
"'&(quot|#34);'i",//替換HTML實體
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(|#169);'i",
"'peihuo.cn|peihuo.mobi|div|/'",
"'&#(d+);'e");//作為PHP代碼運行

$replace=array("",
"",
"",//"\1",
""",
"&",
"<",
">",
"",
chr(161),
chr(162),
chr(163),
chr(169),
"",
"chr(\1)");
returnpreg_replace($search,$replace,$str);
}

閱讀全文

與php去除html函數相關的資料

熱點內容
解壓音樂排名 瀏覽:383
安卓手機瀏覽器怎麼掃二維碼 瀏覽:715
通達信成本均線源碼 瀏覽:614
可以下載的解壓音頻 瀏覽:564
海賊王怎麼換伺服器 瀏覽:318
計算機上的共享文件夾映射 瀏覽:940
榮耀安裝包在文件夾哪裡 瀏覽:195
機票php源碼 瀏覽:231
linux共享mac 瀏覽:922
中國沒有國外的伺服器地址 瀏覽:759
為什麼退款伺服器連接錯誤 瀏覽:557
android簡訊存儲位置 瀏覽:972
unix網路編程卷4 瀏覽:808
找靚機app下單什麼時候發貨 瀏覽:413
android一個應用兩個進程 瀏覽:803
linux硬碟復制 瀏覽:808
php圖片伺服器搭建 瀏覽:801
下載壓縮文件怎麼打開 瀏覽:194
新建文件夾叫什麼名字 瀏覽:567
windows20的開機命令 瀏覽:335