導航:首頁 > 凈水問答 > php過濾html標簽以及裡面的內容

php過濾html標簽以及裡面的內容

發布時間:2020-12-26 20:29:21

1. php截取某個html標簽裡面內容的正則表達式,標簽如下

$matches=array();
$b='<spanid="oldtitle"><strong>艾布拉姆斯他專爹屬</strong></span>';
preg_match_all('(<spanid="oldtitle"><strong>(.*)</strong></span>)',$b,$matches);
print_r($matches);

2. php含有html標簽的內容需要過濾

防禦XSS攻擊,最簡單粗暴的做法就是用htmlspecialchars把特殊字元(&,",',<,>)替換為HTML實體(&"'<>)後輸出.防禦XSS攻擊專,最復雜的做法屬就是自己寫正則過濾,不過還好有HTMLPurifier庫,除了能過濾XSS代碼,還能把不完整的標簽補全或者去掉.

<?php
# http://htmlpurifier.org/download
require dirname(__FILE__).'/htmlpurifier/library/HTMLPurifier.auto.php';
$purifier = new HTMLPurifier();
echo $purifier->purify($html);

3. PHP 過濾HTML中除了img標簽外其它所有標簽,同時保留標簽內容,但<script>標簽內的內容都清除。

提供實例:
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";
// 允許 <p> 和 <a>
echo strip_tags($text, '<p><a>');
?>
以上常式會輸出:版
Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>

具體做權法:
<?php
echo strip_tags($text, 'img');
?>

4. php 正則匹配HTML標簽中間內容

$str=你要匹配的字元串

$regex1="/.*?<a .*?href=\"(.*?)\" .*? style=\".*?\">.*?/";
$regex2="/.*?<img src=\"(.*?)\" \/>.*?/";
$regex3="/.*?<a.*?target=\"_blank\">\s*(.*?)\s*<\/a>.*?/";
$regex4="/.*?<span class=\"content\">(.*?)<\/span>.*?/";
if(preg_match_all($regex1, $str, $matches)){
var_mp($matches[1]);
}
if(preg_match_all($regex2, $str, $matches)){
var_mp($matches[1]);
}
if(preg_match_all($regex3, $str, $matches)){
var_mp($matches[1]);
}
if(preg_match_all($regex4, $str, $matches)){
var_mp($matches[1]);
}
不行再專問屬

5. php 正則表達式去掉指定html標簽中所有的子標簽

<?php

$string="<php>1<p>02</p><p>888</p></php><p>123</p><php><p>234</p></php>";
$pattern = '/<php>([\s\S]*)<\/php>/iU';
preg_match_all($pattern,$string,$d);

foreach ($d[1] as $val) {
$string = str_replace($val,strip_tags($val),$string);
}

echo $string;

?>

6. php中刪除html標簽里的屬性(可用正則表達式)

$str = '<span style=\"font-family:\'Arial\',\'sans-serif\';font-size:9pt;\">test</span>';
$str=stripslashes($str);
$str = preg_replace('/<([a-z]+)\s+[^>]*>/is', '<$1>', $str);

echo htmlspecialchars($str)."<br/>";

7. 用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]*

8. php正則表達式截取HTML標簽中的內容

header('content-type:text/html;charset=utf-8');
$str='<li><ahref="/news1397/"title="1827年3月5日義大利物理學家伏打逝世">1827年3月5日義大利物理學家伏打逝世</a></li>

<li><ahref="/news1398/"title="1871年3月5日波蘭回女革命家盧森堡誕答辰">1871年3月5日波蘭女革命家盧森堡誕辰</a></li>

<li><ahref="/news1399/"title="1886年3月5日董必武誕辰">1886年3月5日董必武誕辰</a>(圖)</li>';
preg_match_all('/<a.*>(.*)</a>/im',$str,$matches);
var_mp($matches[1]);

9. 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

10. 求php 過濾html標簽 但不過濾標簽裡面的文字 的代碼

<?php
$str='<ahref="#">href</a>';
//echohtmlspecialchars($str);
echostrip_tags($str);
?>

閱讀全文

與php過濾html標簽以及裡面的內容相關的資料

熱點內容
除垢可以用草酸嗎 瀏覽:835
去除水壺中水垢 瀏覽:611
什麼自來水凈化器好用 瀏覽:910
沼氣廢水怎麼做 瀏覽:94
目前武漢市有哪些污水處理廠 瀏覽:247
反滲透去除什麼離子 瀏覽:566
建築業污水處理費怎麼徵收 瀏覽:666
軒逸經典的空調濾芯是什麼樣的 瀏覽:81
凈水器上濾盒放在什麼位置 瀏覽:719
凈水器水滿了怎麼辦 瀏覽:525
為什麼純凈水是189升 瀏覽:176
櫻慈凈水器怎麼更換濾芯 瀏覽:771
廢水主要來源於 瀏覽:539
污水c0dm9l代表什麼 瀏覽:893
陶氏反滲透膜計算方法 瀏覽:863
煎煮法蒸餾法 瀏覽:456
如何使用凈水器濾芯 瀏覽:552
氯丁膠與樹脂膠能硫化嗎 瀏覽:755
凈化器除甲醛什麼最好 瀏覽:418
飲水機綠燈一起亮是什麼原因 瀏覽:60