当前位置:首页 » 净水方式 » aspnet过滤html标签

aspnet过滤html标签

发布时间: 2021-03-09 02:12:37

『壹』 求一过虑字符串中HTML标签及特殊符号的方法(asp.net)

using System;

/// <summary>
/// CleanString 的摘要说明。
/// </summary>
public sealed class CleanString
{

public static string htmlInputText( string inputString )//HTML过滤输入字符串
{
if ((inputString != null) && (inputString != String.Empty ))
{
inputString = inputString.Trim();
inputString = inputString.Replace("'",""");
inputString = inputString.Replace("<","<");
inputString = inputString.Replace(">",">");
inputString = inputString.Replace(" "," ");
inputString = inputString.Replace("\n","<br>");
return inputString.ToString();
}
return "";
}

public static string htmlOutputText( string inputString )//HTML还原字符串
{
if ((inputString != null) && (inputString != String.Empty ))
{
inputString = inputString.Trim();
inputString = inputString.Replace(""","'");
inputString = inputString.Replace("<","<");
inputString = inputString.Replace(">",">");
inputString = inputString.Replace(" "," ");
inputString = inputString.Replace("<br>","\n");
return inputString.ToString();
}
return "";
}

}

把这个写成一个类 想在哪里用随便掉就好了!很方便的...!

『贰』 asp.net中 去掉html标签

public static string ClearHtml(string strHtml)
{
if (!string.IsNullOrEmpty(strHtml))
{
Regex r = null;
Match m = null;

r = new Regex(@"<\/?[^>]*>", RegexOptions.IgnoreCase);
for (m = r.Match(strHtml); m.Success; m = m.NextMatch())
{
strHtml = strHtml.Replace(m.Groups[0].ToString(), "");
}
}
return strHtml;
}

『叁』 asp.net中在 web.config配置什么可以 过滤HTML标签

在web.config中增加一条<pages validateRequest="false"/>禁用请求验证,这样在提交带有html代码数据的时候就不会报错。

『肆』 asp.net求真正能去掉html标签的方法

你先要把实体字符转成html标签,然后再用正则去掉标签

『伍』 ASP.NET如何去掉一个字符串中的html标签只留文字

如果你的文字是在value 或者text 属性里面,你可以直接找到这些属性,然后取值。
如果你的文字是在标记中间。可以通过查找>< 符号来获取到文字。
另外,如果你这里的格式有规律,那也可以通过正则来匹配。

『陆』 怎样在asp.net(C#)中或用JS去掉html标签

我这个不用JS ,用正则匹配,效率也是很高的,调用也很方便,我一直用到现在,希望对您也有用
/// <summary>
/// 移除HTML标签
/// </summary>
/// <param name="HTMLStr">HTMLStr</param>
public static string ParseTags(string HTMLStr)
{
return System.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>", "");
}

『柒』 asp.net如何过滤掉html代码

Asp.net中如何过滤html,js,css代码
以下为引用的内容:

#region/// 过滤html,js,css代码
/// <summary>
/// 过滤html,js,css代码
/// </summary>
/// <param name="html">参数传入</param>
/// <returns></returns>
public static string CheckStr(string html)
{
System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
html = regex1.Replace(html, ""); //过滤<script></script>标记
html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
html = regex4.Replace(html, ""); //过滤iframe
html = regex5.Replace(html, ""); //过滤frameset
html = regex6.Replace(html, ""); //过滤frameset
html = regex7.Replace(html, ""); //过滤frameset
html = regex8.Replace(html, ""); //过滤frameset
html = regex9.Replace(html, "");
html = html.Replace(" ", "");
html = html.Replace("</strong>", "");
html = html.Replace("<strong>", "");
return html;
}
#endregion
#region /// 过滤p /p代码
/// <summary>
/// 过滤p /p代码
/// </summary>
/// <param name="html">参数传入</param>
/// <returns></returns>
public static string InputStr(string html)
{
html = html.Replace(@"\<img[^\>]+\>", "");
html = html.Replace(@"<p>", "");
html = html.Replace(@"</p>", "");
return html;
}
#endregion

『捌』 asp.net 过滤html代码

默认是禁止包含有HTML标签的POST请求,设置 ValidateRequest="false" 就可以了

比如:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits=YourNameSpace.YourClassName" ValidateRequest="false" %>

『玖』 ASP.NET过滤html标签的几种常用方法

没有几种.
原理就是删掉正则匹配到的html标签
至于删除用repalce还是remove我觉得没那么重要了就..
html标签的正则,网上抄的不知道对不对:
"<(.[^>]*)>"

『拾』 asp.net 怎么过滤html标记

前台我放一个label和button 给button加了个点击事件在后台导入using System.Text.RegularExpressions;命名空间具体代码 protected void Button1_Click(object sender, EventArgs e) { string a = "<html><br><hr>ffwe<h1>32e4</ht>"; string strip = StripHTML(a); this.Label1.Text = strip;} public static string StripHTML(string strHtml) { string[] aryReg ={ @"<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);", @"&(|#169);", @"&#(\d+);", @"-->", @"<!--.*\n" }; string[] aryRep = { "", "", "", "\"", "&", "<", ">", " ", "\xa1",//chr(161), "\xa2",//chr(162), "\xa3",//chr(163), "\xa9",//chr(169), "", "\r\n", "" }; string newReg = aryReg[0]; string strOutput = strHtml; for (int i = 0; i < aryReg.Length; i++) { Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase); strOutput = regex.Replace(strOutput, aryRep[i]); } strOutput.Replace("<", ""); strOutput.Replace(">", ""); strOutput.Replace("\r\n", ""); strOutput.Replace(" ", " "); return strOutput; } 经测试可用 最终效果

热点内容
丁度巴拉斯情人电影推荐 发布:2024-08-19 09:13:07 浏览:886
类似深水的露点电影 发布:2024-08-19 09:10:12 浏览:80
《消失的眼角膜》2电影 发布:2024-08-19 08:34:43 浏览:878
私人影院什么电影好看 发布:2024-08-19 08:33:32 浏览:593
干 B 发布:2024-08-19 08:30:21 浏览:910
夜晚看片网站 发布:2024-08-19 08:20:59 浏览:440
台湾男同电影《越界》 发布:2024-08-19 08:04:35 浏览:290
看电影选座位追女孩 发布:2024-08-19 07:54:42 浏览:975
日本a级爱情 发布:2024-08-19 07:30:38 浏览:832
生活中的玛丽类似电影 发布:2024-08-19 07:26:46 浏览:239