当前位置:首页 > PHP教程 >

PHP去除html标签

发布时间:2024-01-10 12:20:36 作者:佚名 阅读:(18)

作为一个站长,有时候我们采集过来的内容中难免会带有html标签,如果有太多的标签会影响之后的数据分析或提取,所以需要过滤掉一些多余的html标签,云梦编程为你介绍一下如何去除html标签,有需要的小伙伴可以参考一下:

PHP去除html标签_PHP清除html格式

方法一:

$content = preg_replace("/]*>/i", "", $content);  
$content = preg_replace("/<\/a>/i", "", $content);   
$content = preg_replace("/]*>/i", "", $content);  
$content = preg_replace("/<\/div>/i", "", $content);      
$content = preg_replace("//i", "", $content);//注释内容  
$content = str_replace("\r","",$content);//过滤换行
$content = str_replace("\n","",$content);//过滤换行
$content = str_replace("\t","",$content);//过滤换行
$content = str_replace("\r\n","",$content);//过滤换行
$content = preg_replace("/\s+/", " ", $content);//过滤多余回车
$content = preg_replace("/<[ ]+/si","<",$content); //过滤<__("<"号后面带空格)
$content = preg_replace("/<\!--.*?-->/si","",$content); //过滤html注释
$content = preg_replace("/<(\!.*?)>/si","",$content); //过滤DOCTYPE
$content = preg_replace("/<(\/?html.*?)>/si","",$content); //过滤html标签
$content = preg_replace("/<(\/?head.*?)>/si","",$content); //过滤head标签
$content = preg_replace("/<(\/?meta.*?)>/si","",$content); //过滤meta标签
$content = preg_replace("/<(\/?body.*?)>/si","",$content); //过滤body标签
$content = preg_replace("/<(\/?link.*?)>/si","",$content); //过滤link标签
$content = preg_replace("/<(\/?form.*?)>/si","",$content); //过滤form标签
$content = preg_replace("/cookie/si","COOKIE",$content); //过滤COOKIE标签

方法二:strip_tags

strip_tags($str) 去掉 HTML 及 PHP 的标记
语法: string strip_tags(string str);
传回值: 字串
函式种类: 资料处理
内容说明 :
解析:本函式可去掉字串中包含的任何 HTML 及 PHP 的标记字串。若是字串的 HTML 及 PHP 标签原来就有错,例如少了大于的符号,则也会传回错误。这个函数和 fgetss() 有着相同的功能

例子

echo strip_tags("Hello world!"); # Hello world!

方法三:htmlspecialchars

这个函数把html中的标签转换为html实体,博客的代码展示就必须使用这个函数,要不贴出来的代码就会被执行了。
预定义的字符是:
& (和号) 成为 &
” (双引号) 成为 ”
‘ (单引号) 成为 ‘
< (小于) 成为 < > (大于) 成为 >


以上就是云梦编程为大家介绍的关于PHP清除html标签方法的全部内容了,了解更多相关文章请关注云梦编程网!

© 2023 - 云梦编程网 版权所有 鲁ICP备2021017318号-4