过滤HTML代码
public static string FilterHtml(string string_include_html)
{
string[] HtmlRegexArr ={
#region Html 正则数组
@"<script[^>]*?>.*?</script>",
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
@"([\r\n])[\s]+",
@"&(quot|#34);",
@"&(amp|#38);",
@"&(lt|#60);",
@"&(gt|#62);",
@"&(nbsp|#160);",
@"&(iexcl|#161);",
@"&(cent|#162);",
@"&(pound|#163);",
@"&(copy|#169);",
@"&#(\d+);",
@"-->",
@"<!--.*\n"
#endregion
};
string[] HtmlReplaceArr = {
#region 替换Html字符
"",
"",
"",
"\"",
"&",
"<",
">",
" ",
"\xa1",
"\xa2",
"\xa3",
"\xa9",
"",
"\r\n",
""
#endregion
};
string string_no_html = null;
for (int i = ; i < HtmlRegexArr.Length; i++)
{
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(HtmlRegexArr[i], System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string_no_html = regex.Replace(string_include_html, HtmlReplaceArr[i]);
}
string_no_html.Replace("<", "");
string_no_html.Replace(">", "");
string_no_html.Replace("\r\n", "");
return string_no_html;
}
以上来至网络,但个人认为还是不行。故有以下自己写的:
/// <summary>
/// 将Html标签转化为空
/// </summary>
/// <param name="strHtml">待转化的字符串</param>
/// <returns>经过转化的字符串</returns>
public static string GetStringNoHtml(string string_include_html)
{
if (String.IsNullOrEmpty(string_include_html))
{
return "";
}
else
{
string_include_html = string_include_html.Replace("<BR>", "\r\n").Replace("<br>", "\r\n");
//第一种
string string_no_html = System.Text.RegularExpressions.Regex.Replace(string_include_html, @"(<script[^>]*?>.*?</script>)|(<(.[^>]*)>)", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return string_no_html.Replace(" ", " ");
//第二种
//return System.Text.RegularExpressions.Regex.Replace(string_include_html, @"(<script[^>]*?>.*?</script>)|(<(.[^>]*)>)|( )", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
}
}
过滤HTML代码的更多相关文章
- php 安全过滤函数代码
php 安全过滤函数代码,防止用户恶意输入内容. //安全过滤输入[jb] function check_str($string, $isurl = false) { $string = preg_r ...
- 取缔Chrome装载电脑管家的广告过滤脚本代码
今天Chrome调试脚本.加载在下面的脚本中找到的内容: /* 电脑管家chrome 广告过滤 */ var GJAD_CS = { elemhideElt : null, setElemhideCS ...
- c# 过滤HTML代码 源代码,案例 下载
#region 过滤HTML代码 //替换掉html字符,只显示文字信息. public string replaceHtmlCode(string Htmlstring) { Htmlstring ...
- yii过滤xss代码,防止sql注入
作者:白狼 出处:www.manks.top/article/yii2_filter_xss_code_or_safe_to_database 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明 ...
- ItemCF_基于物品的协同过滤_MapReduceJava代码实现思路
ItemCF_基于物品的协同过滤 1. 概念 2. 原理 如何给用户推荐? 给用户推荐他没有买过的物品--103 3. java代码实现思路 数据集: 第一步:构建物品的同现矩阵 第 ...
- Maven打包时过滤测试代码或指定特定的测试类(maven-surefire-plugin)
1.过滤整个测试代码,可以直接在命令行上指定 mvn clean install -Dmaven.test.skip=true 提示:以上为举例,具体的构建阶段可以自定义,其中maven.test.s ...
- asp.net c#过滤html代码,净化DIV SPAN等
public static string GetSafeHtml(string val) { if (string.IsNullOrEmpty(val)) { return string.Empty; ...
- 解决Ext.TextField的AllowBlank不能过滤空格代码
Ext过滤空格 重写了组件... Ext.apply(Ext.form.TextField.prototype, { validator : function(text) { if (this.all ...
- Yii-CHtmlPurifier- 净化器的使用(yii过滤不良代码)
1. 在控制器中使用: public function actionCreate() { $model=new News; $purifier = new CHtmlPurifier(); $puri ...
- PHP-preg_replace过滤字符串代码
$str=preg_replace("/\s+/", " ", $str); //过滤多余回车 $str=preg_replace("/& ...
随机推荐
- [maven] 使用Nexus创建maven私有仓库
1.为什么需要maven私有仓库? 从Maven中央仓库下载所需的jar包,需要外网的支持.如果公司不能上外网的话则不能从中央仓库下载所需jar包,公司网速慢的时候也会影响项目构建的速度.用户可以用n ...
- redis初步入门
http://blog.csdn.net/u014419512/article/details/25693425 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 环境 安装 ...
- iTunes Affiliate Resources
https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/
- yii2.0高级框架配置时打开init.bat秒退的解决方法 (两种方法)
第一种: 这几天刚接触到yii2.0框架,在配置advanced版本时运行init.bat初始化文件时老是闪退: 用cmd运行该文件时显示:The OpenSSL PHP extension is r ...
- html 选择图片后马上展示出来
document.getElementById('file4').onchange = function(evt) { // 如果浏览器不支持FileReader,则不处理 if (!window.F ...
- JavaScript中的逗号运算符
JavaScript逗号运算符 阅读本文的前提,明确表达式.短语.运算符.运算数这几个概念. 所谓表达式,就是一个JavaScript的“短语”,JavaScript解释器可以计算它,从而生成一个值 ...
- innerHTML,innerText,outHTML,outText区别
<p><div id="div" style="background-color:#ff9966;border:1px #ff0000 dashed;& ...
- React Native组件之Text
React Native组件之Text相当于iOS中的UILabel. 其基本属性如下: /** * Sample React Native App * https://github.com/face ...
- TCP 状态机
TCP 状态机 TCP 协议的操作可以使用一个具有 11 种状态的有限状态机( Finite State Machine )来表示,图 3-12 描述了 TCP 的有限状态机,图中的圆角矩形表示状态, ...
- PHP Html5上传大文件
html前台代码: <form id="upload_form" name="upload_form" action="javascript:i ...