过滤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("/& ...
随机推荐
- css样式设计
1.行内元素(图片.文本)水平居中 通过给父元素设置 text-align:center html代码: <body> <div class="txtCenter" ...
- [笔记]JavaScript获得对象属性个数的方法
//扩展对象的count方法 Object.prototype.count = ( Object.prototype.hasOwnProperty(‘__count__’) ) ? function ...
- 【原创】Mac os 10.10.3 安装xgboost
大家用的比较多的是Linux和windows,基于Mac os的安装教程不多, 所以在安装的过程中遇到很多问题,经过较长时间的尝试,可以正常安装和使用, [说在前面]由于新版本的Os操作系统不支持op ...
- 探索C++的秘密之详解extern "C",这就是为什么很多.lib被我们正确调用确总是无法解析的。
(转载,绝对的有用) lib被我们正确调用确总是无法解析.这是C++编译和C编译的区别 时常在cpp的代码之中看到这样的代码: #ifdef __cplusplus extern "C&qu ...
- 张艾迪(创始人):创始人故事无限N个
世界第一女孩+世界第一互联网女孩 创始人故事无限N个 全球第一互联网女孩EidyZhang艾迪.张 The World No.1 Girl :Eidyzhang The World No.1 Inte ...
- flash透明效果代码分享~~~
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...
- 如何通过pid快速找出进程的路径
[carlton@oc3408554812 Desktop]$ top top - 09:35:06 up 32 min, 2 users, load average: 1.49, 1.56, 1 ...
- LeetCode 176 Second Highest Salary mysql,select 嵌套 难度:1
https://leetcode.com/problems/second-highest-salary/ Write a SQL query to get the second highest sal ...
- PHP 面向对象编程(2)
一些内建方法: class Person { public $isAlive = true; function __construct($name) { //这里我们创建了一个name的属性 $thi ...
- 两个小的java程序,用于练习java基本语法
1.输入两个数,求其加减乘除.用窗口的形式呈现 import javax.swing.JOptionPane; public class JJCC { public static void main( ...