1. php截取某个html标签里面内容的正则表达式,标签如下
$matches=array();
$b='<spanid="oldtitle"><strong>艾布拉姆斯他专爹属</strong></span>';
preg_match_all('(<spanid="oldtitle"><strong>(.*)</strong></span>)',$b,$matches);
print_r($matches);
2. php含有html标签的内容需要过滤吗
防御XSS攻击,最简单粗暴的做法就是用htmlspecialchars把特殊字符(&,",',<,>)替换为HTML实体(&"'<>)后输出.防御XSS攻击专,最复杂的做法属就是自己写正则过滤,不过还好有HTMLPurifier库,除了能过滤XSS代码,还能把不完整的标签补全或者去掉.
<?php
# http://htmlpurifier.org/download
require dirname(__FILE__).'/htmlpurifier/library/HTMLPurifier.auto.php';
$purifier = new HTMLPurifier();
echo $purifier->purify($html);
3. 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');
?>
4. php 正则匹配HTML标签中间内容
$str=你要匹配的字符串
$regex1="/.*?<a .*?href=\"(.*?)\" .*? style=\".*?\">.*?/";
$regex2="/.*?<img src=\"(.*?)\" \/>.*?/";
$regex3="/.*?<a.*?target=\"_blank\">\s*(.*?)\s*<\/a>.*?/";
$regex4="/.*?<span class=\"content\">(.*?)<\/span>.*?/";
if(preg_match_all($regex1, $str, $matches)){
var_mp($matches[1]);
}
if(preg_match_all($regex2, $str, $matches)){
var_mp($matches[1]);
}
if(preg_match_all($regex3, $str, $matches)){
var_mp($matches[1]);
}
if(preg_match_all($regex4, $str, $matches)){
var_mp($matches[1]);
}
不行再专问属
5. php 正则表达式去掉指定html标签中所有的子标签
<?php
$string="<php>1<p>02</p><p>888</p></php><p>123</p><php><p>234</p></php>";
$pattern = '/<php>([\s\S]*)<\/php>/iU';
preg_match_all($pattern,$string,$d);
foreach ($d[1] as $val) {
$string = str_replace($val,strip_tags($val),$string);
}
echo $string;
?>
6. php中删除html标签里的属性(可用正则表达式)
$str = '<span style=\"font-family:\'Arial\',\'sans-serif\';font-size:9pt;\">test</span>';
$str=stripslashes($str);
$str = preg_replace('/<([a-z]+)\s+[^>]*>/is', '<$1>', $str);
echo htmlspecialchars($str)."<br/>";
7. 用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]*
8. php正则表达式截取HTML标签中的内容
header('content-type:text/html;charset=utf-8');
$str='<li><ahref="/news1397/"title="1827年3月5日意大利物理学家伏打逝世">1827年3月5日意大利物理学家伏打逝世</a></li>
<li><ahref="/news1398/"title="1871年3月5日波兰回女革命家卢森堡诞答辰">1871年3月5日波兰女革命家卢森堡诞辰</a></li>
<li><ahref="/news1399/"title="1886年3月5日董必武诞辰">1886年3月5日董必武诞辰</a>(图)</li>';
preg_match_all('/<a.*>(.*)</a>/im',$str,$matches);
var_mp($matches[1]);
9. php 过滤掉html标签及标签内的所有内容
方法一:使用strip_tags()函数
strip_tags() 函数剥去字符串中的 HTML、XML 以及PHP的标签。
使用内案例:
$string = "<p>这里是容潘旭博客</p>"
$newStr = strip_tags($string);
echo $newStr;
方法二:使用str_replace()函数
str_replace() 函数以其他字符替换字符串中的一些字符(区分大小写)
使用案例:
$string = "<p>这里是潘旭博客</p>";
$newStr = str_replace(array("<p>","</p>"),array("",""));
echo $newStr;
另外还有一种是通过正则的方法,请参考:https://panxu.net/article/8385.html
10. 求php 过滤html标签 但不过滤标签里面的文字 的代码
<?php
$str='<ahref="#">href</a>';
//echohtmlspecialchars($str);
echostrip_tags($str);
?>