服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > php教程 >

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

时间:2016-02-17 01:11来源: 作者: 点击:
关于各种PHP去除多余的HTML,Javascrit,Css标签 方法与实现程序,大家可进入参考. 1.不保留任何HTML标签,代码会是这样:echo strip_tags($str); 2.只保留 一个标签的话,只需要将 字符串写到strip_tags的
关于各种PHP去除多余的HTML,Javascrit,Css标签 方法与实现程序,大家可进入参考.
 
1.不保留任何HTML标签,代码会是这样:echo strip_tags($str);  
 
2.只保留
 
一个标签的话,只需要将
 
字符串写到strip_tags的第二个参数中,代码会是这样:echo strip_tags($str, "");  
 
3.我们要保留
 
与…多个标签,只需要将多个标签用空格分隔后写到strip_tags的第二个参数中,代码会是这样:echo strip_tags($str, "");
 
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);   传回值: 字串   函式种类: 资料处理
实例代码如下:
 
$new = htmlspecialchars("/i", '', $content);     
  $content = preg_replace("/]*>/i",'', $content);    
  $content = preg_replace("//i",'', $content); 
  $content = preg_replace("/]*>/i",'', $content);    
  $content = preg_replace("//i",'', $content); 
  $content = preg_replace("/]*>/i",'', $content);    
  $content = preg_replace("/
/i",'', $content); 
  $content = preg_replace("/]*>/i",'', $content);    
  $content = preg_replace("//i",'', $content); 
  $content = preg_replace("/]*>/i",'', $content); 
  $content = preg_replace("//i",'', $content); 
  $content = preg_replace("/]*>/i",'', $content); 
  $content = preg_replace("//i",'', $content); 
  $content = preg_replace("/]*>/i",'', $content); 
  $content = preg_replace("//i",'', $content); 
  $content = preg_replace("/]*>/i",'', $content); 
  $content = preg_replace("//i",'', $content);  
  $content = preg_replace("/]*>/i",'', $content); 
       $content = preg_replace("//i",'', $content); 
  $content = preg_replace("//i",'', $content);//注释内容   
  $content = preg_replace("/]*-->/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( " ","",$content); 
     return $content; 
    } 
(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容