⑴ 求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标签以及指定标签的属性进行过滤