PHP头条
热点:

PHP去除多余的HTML,Javascrit,Css标签


本文章来给大家介绍关于各种PHP去除多余的HTML,Javascrit,Css标签 方法与实现程序,大家可进入参考。

1.不保留任何HTML标签,代码会是这样:echo strip_tags($str);  

2. 只保留<p>一个标签的话,只需要将<p>字符串写到strip_tags的第二个参数中,代码会是这样:echo strip_tags($str, "<p>");  

3. 我们要保留<p>与<b>…多个标签,只需要将多个标签用空格分隔后写到strip_tags的第二个参数中,代码会是这样:echo strip_tags($str, "<p> <b>");

4.保留所有标签,仅仅转义用addslashes(), stripslashes(), htmlspecialchars(), htmlentities(), nl2br() 等函数.

 addslashes(), stripslashes() 一般是入数据库和出库的时候使用,以免变量中存储类似引号这些关键词,这样的话,本来是内容的部分却被数据库识别为标识符来执行,就会引起错误.

 htmlspecialchars() 函数只用来转义少量HTML, &,双引号,大于号和小于号.并不会全部转换成 HTML 所定的 ASCII 转换

 htmlentities() 本函数有点像 htmlspecialchars() 函数,但本函数会将所有 string 的字符都转成 HTML 的特殊字集字符串。不过在转换后阅读网页源代码的方面,会有很多困扰,尤其是网页源代码的中文字会变得不知所云,浏览器上看到的还是正常的。

 

自带函数去除html标记

strip_tags

  去掉 HTML 及 PHP 的标记。

  语法: string strip_tags(string str);

  传回值: 字串

  函式种类: 资料处理

 代码如下 复制代码


<?php 
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES); 
echo $new; 

?> 

函式将特殊字元转成 HTML 的字串格式 ( &....; )。最常用到的场合可能就是处理客户留言的留言版了。

  & (和) 转成 &amp;
  " (双引号) 转成 &quot;
  < (小于) 转成 &lt;
  > (大于) 转成 &gt;
  此函式只转换上面的特殊字元,并不会全部转换成 HTML 所定的 ASCII 转换。

 

这里只替换 html,js,css

 代码如下 复制代码

function get_enhtml($string){
$pattern=array ("'<script[^>]*?>.*?</script>'si",// 去掉 javascript
"'<style[^>]*?>.*?</style>'si",// 去掉 HTML 标记
"'<[/!]*?[^<>]*?>'si",//去掉 HTML 标记
"'<!--[/!]*?[^<>]*?>'si", // 去掉 注释标记
"'([rn])[s]+'", // 去掉空白字符
"'&(quot|#34);'i",
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(d+);'e");
$replace=array ("", "", "", "", "1", "", "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), "chr(1)");
$string=preg_replace($pattern, $replace, $string);
$string=preg_replace("/<(.*?)>/","",$string);
$string=str_replace("n","",$string);
$string=str_replace("r","",$string);
$string=str_replace("  ","",$string);
$string=str_replace("  ","",$string);
return addslashes(trim($string));
}

替换所有html标签

 代码如下 复制代码

function noHTML($content)
    {
     $content = preg_replace("/<a[^>]*>/i",'', $content);  
  $content = preg_replace("/</a>/i", '', $content);   
  $content = preg_replace("/<div[^>]*>/i",'', $content);  
  $content = preg_replace("/</div>/i",'', $content);
  $content = preg_replace("/<font[^>]*>/i",'', $content);  
  $content = preg_replace("/</font>/i",'', $content);
  $content = preg_replace("/<p[^>]*>/i",'', $content);  
  $content = preg_replace("/</p>/i",'', $content);
  $content = preg_replace("/<span[^>]*>/i",'', $content);  
  $content = preg_replace("/</span>/i",'', $content);
  $content = preg_replace("/<?xml[^>]*>/i",'', $content);
  $content = preg_replace("/</?xml>/i",'', $content);
  $content = preg_replace("/<o:p[^>]*>/i",'', $content);
  $content = preg_replace("/</o:p>/i",'', $content);
  $content = preg_replace("/<u[^>]*>/i",'', $content);
  $content = preg_replace("/</u>/i",'', $content);
  $content = preg_replace("/<b[^>]*>/i",'', $content);
  $content = preg_replace("/</b>/i",'', $content);
  $content = preg_replace("/<meta[^>]*>/i",'', $content);
       $content = preg_replace("/</meta>/i",'', $content);
  $content = preg_replace("/<!--[^>]*-->/i",'', $content);//注释内容 
  $content = preg_replace("/<p[^>]*-->/i",'', $content);//注释内容      
  $content = preg_replace("/style=.+?['|"]/i",'',$content);//去除样式  
  $content = preg_replace("/class=.+?['|"]/i",'',$content);//去除样式  
  $content = preg_replace("/id=.+?['|"]/i",'',$content);//去除样式     
  $content = preg_replace("/lang=.+?['|"]/i",'',$content);//去除样式      
  $content = preg_replace("/width=.+?['|"]/i",'',$content);//去除样式   
  $content = preg_replace("/height=.+?['|"]/i",'',$content);//去除样式   
  $content = preg_replace("/border=.+?['|"]/i",'',$content);//去除样式   
  $content = preg_replace("/face=.+?['|"]/i",'',$content);//去除样式
     $content = preg_replace("/face=.+?['|"]/",'',$content);
     $content = preg_replace("/face=.+?['|"]/",'',$content);
     $content=str_replace( "&nbsp;","",$content);
     return $content;
    }

www.phpzy.comtrue/php/454.htmlTechArticlePHP去除多余的HTML,Javascrit,Css标签 本文章来给大家介绍关于各种PHP去除多余的HTML,Javascrit,Css标签 方法与实现程序,大家可进入参考。 1.不保留任何HTML标签,代码会是这样:echo strip_tags...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐