⑴ 求php 過濾html標簽 但不過濾標簽裡面的文字 的代碼
<?php
$str='<ahref="#">href</a>';
//echohtmlspecialchars($str);
echostrip_tags($str);
?>
⑵ php 提取替換指定html內的標簽
試編寫代碼供參考:
<?php
$content=<<<TTTT
<strong>開心</strong>
<li>數列1</li>
<li>數列2</li>
<li>數列3</li>
<strong>無聊</strong>
<li>數列4</li>
<li>數列5</li>
<li>數列6</li>
<strong>興奮</strong>
<li>數列7</li>
<li>數列8</li>
<li>數列9</li>
<strong>沮喪</strong>
<li>數列10</li>
<li>數列11</li>
<li>數列12</li>
TTTT;
/*
$pattern='%<strong>(.*?)</strong>%i';
preg_match_all($pattern,$content,$matches,PREG_PATTERN_ORDER);
echo'問題一:提取標簽內的內容的前3個:<br/>'." ";
echo$matches[1][0].','.$matches[1][1].','.$matches[1][2].'<br/><br/>';
echo" "." ";
echo'問題二:提取標簽內的所有內容,並加序列號和html標簽:<br/>'." ";
for($i=0;$i<count($matches[1]);$i++){
echo'<li>'.($i+1).'、'.$matches[1][$i].'</li>'." ";
}
echo'<br/>';
echo" ";
echo'問題三:替換成:<br/>'." ";
$pattern='%<strong>(.*?)</strong>%i';
$temp=preg_replace($pattern,'</ul><strong>1</strong><ul>',$content);
$temp=substr($temp,5).'</ul>';
echo$temp;
*/
$index=0;
functiondoReplace($matches)
{
global$index;
$index++;
if($index<2){
return$index.'.'.$matches[0].'<ul>';
}else{
return'</ul>'.$index.'.'.$matches[0].'<ul>';
}
}
echo'問題三1:在每一個替換的strong前面也加上序列號:<br/>'." ";
echo" ";
$pattern='%<strong>(.*?)</strong>%i';
$temp=preg_replace_callback($pattern,'doReplace',$content);
echo$temp.'</ul>';
?>
運行截圖:
⑶ 用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標簽,使用什麼函數
strip_tags — 從字元串中去除 HTML 和 PHP 標記
語法:
string strip_tags ( string $str [, string $allowable_tags ] )
該函數返回給定的字元串 str 去除空字元、回HTML 和 PHP 標記後的結果。答
參數:
str 要去除的字元串
allowable_tags 可選參數,指定不被去除的字元列表。
例如:
$str = '<a href="" title="">測試</a>';
echo strip_tags($str);
結果:
測試
⑸ php 正則過濾掉 指定的a標簽
<?php
header("Content-type: text/html; charset=utf-8");
$content = '<a class="qc" href="/car">汽車</a>
<a class="db" href="/car">大巴</a>
<a class="qc" href="/car">汽車</a>';
$regex = array('#<a class="qc" href="/car">(.*)</a>#i'=>'$1');
$content = preg_replace(array_keys($regex), array_values($regex), $content);
echo $content;
⑹ php正則表達式過濾某些HTML標簽代碼
如果只要
<b>
標簽,不用「過濾」的方法,用「提取」的方法更簡單。
$str
=
'<img
src="xxx"><b>aaa</b><br>\n<b>b\nbb</b><span
style="color:#FF0000;">yyy</span>';
$pattern
=
'/<b>(((?!<\/b>).)*)<\/b>/mi';
preg_match_all($pattern,
$str,
$matches,
PREG_SET_ORDER);
print_r($matches);
輸出
Array
(
[0]
=>
Array
(
[0]
=>
<b>aaa</b>
[1]
=>
aaa
[2]
=>
a
)
[1]
=>
Array
(
[0]
=>
<b>b\nbb</b>
[1]
=>
b\nbb
[2]
=>
b
)
)
$matches[0][0],$matches[1][0]
是你想要的結果?
⑺ 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');
?>
⑻ php過濾多餘html標簽的代碼!
php過濾多餘html標簽的代碼!
nction filterhtml($str)
{
$str=stripslashes($str);
$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.*?)>(.*?)(\/nofr......年年順景則源廣 歲歲平安福壽多 吉星高照
⑼ 能用PHP 去掉所有html標簽里的部分屬性嗎
PHP也支持正則表達式,通過正則表達式可以對指定的HTML標簽以及指定標簽的屬性進行過濾