導航:首頁 > 編程語言 > php標簽過濾

php標簽過濾

發布時間:2024-10-28 02:37:17

『壹』 求一個php簡單的過濾除<br>,<p>,<style>html標簽的正則或方法

調用下面函數,想去掉什麼標簽,就去掉什麼
<?php
/**
* 去掉指定的html標簽
* @param array $string
* @param bool $str
* @return string
*/
function _strip_tags($tagsArr,$str) {
foreach ($tagsArr as $tag) {
$p[]="/(<(?:\/".$tag."|".$tag.")[^>]*>)/i";
}
$return_str = preg_replace($p,"",$str);
return $return_str;
}

$str = "<b>您好</b><input type='text' name='' /><a href='http://www..com'>網路一下,你就知道</a>";
echo _strip_tags(array("b", "input", "a"),$str); #去掉 B 標簽和 INPUT 標簽
?>

『貳』 用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 正則過濾掉 指定的a標簽

我這個更好
<?php
$str='<a class="qc" href="/car">汽車</a><a class="db" href="/car">大巴</a><a class="qc" href="/car">汽車</a>';
$str=preg_replace("/<a class=\"qc\" href=\"(.*)\">(.*)<\\/a>/iU","$2",$str); //過濾script標簽
echo $str;
?>

『肆』 php含有css代碼的變數。怎麼過濾掉css代碼呢 求高手解答。 。。

一般css代碼都保存在標簽<style></style>之間,那麼正則表達式如下:

$pa = '%<style(.*?)</style>%si';

下面的正則表達式,是匹配html中所有標簽的,你可以進行替換,最終得到html的文本內容:

'%<(style|script)[^<>]*>.*?</\1>|</?[a-z][a-z0-9]*[^<>]*>|<!--.*?-->%si'

閱讀全文

與php標簽過濾相關的資料

熱點內容
安卓桌面工具怎麼刪除 瀏覽:54
外六角螺絲套頭演算法 瀏覽:838
程序員特殊招數是什麼意思 瀏覽:351
描述加密過程 瀏覽:844
我的世界如何開mod伺服器 瀏覽:904
人體寫生pdf 瀏覽:317
android簡訊驗證碼倒計時 瀏覽:641
排課走班源碼 瀏覽:222
程序員剛畢業去了小公司有發展嗎 瀏覽:90
速騰怎麼安裝安卓手機互聯 瀏覽:143
linux設備驅動程序代碼 瀏覽:301
伺服器的功耗怎麼看 瀏覽:651
app組件哪裡找 瀏覽:87
androidqq紅包 瀏覽:412
伺服器如何傳輸 瀏覽:456
如何快速將多個文件夾快速解壓縮 瀏覽:114
程序員睡前都在想什麼 瀏覽:37
少兒編程技能培訓心得 瀏覽:458
白命令 瀏覽:816
headfirstjavapdf 瀏覽:552