本文章来给大家介绍关于各种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
实例代码如下:
- <!–?php
- $new = htmlspecialchars("Test", ENT_QUOTES);
- echo $new;
- ?>
函式将特殊字元转成 HTML 的字串格式 ( &….; ).最常用到的场合可能就是处理客户留言的留言版了.
此函式只转换上面的特殊字元,并不会全部转换成 HTML 所定的 ASCII 转换.
这里只替换 html,js,css
实例代码如下:
- function get_enhtml($string){
- $pattern=array ("'<script[^>]*?>.*?</script>'si",// 去掉 javascript
- "']*?>.*?'si",// 去掉 HTML 标记
- "'<[/!]*?[^<>]*?>'si",//去掉 HTML 标记
- "'{C}{C}<!–[/!]*?[^]*?>'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(“/]*>/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("/
]*–>/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;
- }
波比源码 » PHP去除多余的HTML,Javascrit,Css标签
Good info. Lucky me I reach on your website by accident, I bookmarked it. Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…
Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Hacklink , Hacklink panel , Hacklink al Hacklink panel