導航:首頁 > 編程語言 > 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標簽過濾相關的資料

熱點內容
mis伺服器雲平台 瀏覽:275
為什麼感覺蘋果網速比安卓慢 瀏覽:950
編譯工具路徑是啥 瀏覽:511
雲伺服器如何安裝網站程序 瀏覽:875
單片機藝術 瀏覽:312
雲伺服器寶塔教程 瀏覽:560
用命令怎麼上傳文件到雲伺服器 瀏覽:765
映射網路盤命令 瀏覽:671
貸款車期滿怎麼解壓 瀏覽:923
流量儀表演算法 瀏覽:765
儲能模塊是什麼伺服器 瀏覽:268
安娜pdf 瀏覽:563
安卓手機如何將電腦軟體導入 瀏覽:236
hadoop壓縮配置 瀏覽:591
Java關閉jar 瀏覽:521
加密過的文件打不開也復制不了 瀏覽:830
程序員E4 瀏覽:365
java編程思想看完 瀏覽:844
美國股票加密技術 瀏覽:382
阿里雲計算不需要伺服器 瀏覽:91