導航:首頁 > 編程語言 > php過濾所有html

php過濾所有html

發布時間:2022-07-28 04:17:55

❶ 用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如何過濾編輯器的html標簽

選擇1.將特殊符號進行轉換,可以用htmlspecialchars把<變為「<」等
選擇2.用正則表達式替換,將標簽都刪除:
$content=preg_replace('/\<.+?\>/','',$content);

❸ php文件輸出如何過濾掉html,代碼如下

<b>asasasas</b>這個html標簽是加粗標簽,如果你想在瀏覽器上顯示的是加粗的asasasas就直接輸出
<?php
echo "<b>asasasas</b>";

?>

如果你想輸出的<b>asasasas</b>這個字元串的話呢
<?php

echo htmlspecialchars("<b>asasasas</b>");

?>

❹ php過濾多餘html標簽的代碼!

$str = 」 This line contains\tliberal \r\n use of whitespace.\n\n」;

$str = trim($str);// 首先去掉頭尾空格

$str = preg_replace(』/\s(?=\s)/』, 『』, $str);// 接著去掉兩個空格以上的

$str = preg_replace(』/[\n\r\t]/』, 『 『, $str);// 最後將非空格替換為一個空格

使用上面的例子可以去掉所有多餘的空格。

首先使用TRim()去頭尾空格,

接著用preg_replace()去掉重復的空格。

當中的(?=)表示只匹配後面的空格跟隨前面的空格的空格

❺ 求一個簡單的php語句,截取字元,過濾html代碼。

去除html標簽
function strip_tags($string, $replace_with_space = true)
{
if ($replace_with_space) {
return preg_replace('!<[^>]*?>!', ' ', $string);
} else {
return strip_tags($string);
}
}

截取字元函數(匹配各種編碼)
function truncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false){
if ($length == 0)
return '';

if (is_callable('mb_strlen')) {
if (mb_detect_encoding($string, 'UTF-8, ISO-8859-1') === 'UTF-8') {
// $string has utf-8 encoding
if (mb_strlen($string) > $length) {
$length -= min($length, mb_strlen($etc));
if (!$break_words && !$middle) {
$string = preg_replace('/\s+?(\S+)?$/u', '', mb_substr($string, 0, $length + 1));
}
if (!$middle) {
return mb_substr($string, 0, $length) . $etc;
} else {
return mb_substr($string, 0, $length / 2) . $etc . mb_substr($string, - $length / 2);
}
} else {
return $string;
}
}
}
// $string has no utf-8 encoding
if (strlen($string) > $length) {
$length -= min($length, strlen($etc));
if (!$break_words && !$middle) {
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1));
}
if (!$middle) {
return substr($string, 0, $length) . $etc;
} else {
return substr($string, 0, $length / 2) . $etc . substr($string, - $length / 2);
}
} else {
return $string;
}
}

綜合就是
$arc=strip_tags($arc);
$arc=truncate($arc,200)

❻ PHP正則表達式過濾HTML代碼的問題

一定要用正則么?PHP的函數可以么?
<?php
$str = '<br />sdfasdfsdfa<a>sdfasdf<br>dsfasdfasdf<li>sdfa';
$str = strip_tags($str, '<br>');
echo $str;
?>
會比用正則效率高不少

❼ 能用PHP 去掉所有html標簽里的部分屬性嗎

PHP也支持正則表達式,通過正則表達式可以對指定的HTML標簽以及指定標簽的屬性進行過濾

閱讀全文

與php過濾所有html相關的資料

熱點內容
伺服器有什麼危害 瀏覽:256
飢荒怎麼開新的獨立伺服器 瀏覽:753
文件夾變成了 瀏覽:560
linuxpython綠色版 瀏覽:431
怎麼下載小愛同學音箱app 瀏覽:554
python佔位符作用 瀏覽:76
javajdbcpdf 瀏覽:543
php網頁模板下載 瀏覽:192
python試講課pygame 瀏覽:409
安居客的文件夾名稱 瀏覽:677
家裡伺服器如何玩 瀏覽:451
網站源碼使用視頻 瀏覽:748
stc89c52單片機最小系統 瀏覽:452
郵件安全證書加密 瀏覽:416
雲伺服器如何訪問百度 瀏覽:279
常州電信伺服器dns地址 瀏覽:839
用小方塊製作解壓方塊 瀏覽:42
圖像壓縮編碼實現 瀏覽:68
特色功能高拋低吸線副圖指標源碼 瀏覽:71
西方哲學史pdf羅素 瀏覽:874