如何过滤HTML的标记 急!

jce195447 2010-10-18 01:14:00
RT
...全文
147 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
snihcel 2010-10-18
  • 打赏
  • 举报
回复

public static string NoHTML(string Htmlstring)
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "",
RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "",
RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring =System.Web.HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

return Htmlstring;
}
龍月 2010-10-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wuyq11 的回复:]
using System.Text.RegularExpressions;
Regex.Replace(str,@"<[^> ]+>",""); 过滤所有html.
<pages validateRequest="false" >
[/Quote]

+1
SvenCows 2010-10-18
  • 打赏
  • 举报
回复
Server.HtmlEncode();
wuyq11 2010-10-18
  • 打赏
  • 举报
回复
using System.Text.RegularExpressions;
Regex.Replace(str,@"<[^> ]+>",""); 过滤所有html.
<pages validateRequest="false" >
jce195447 2010-10-18
  • 打赏
  • 举报
回复
从客户端(TextBox7="<sss>")中检测到有潜在危险的 Request.Form 值
fellowcheng 2010-10-18
  • 打赏
  • 举报
回复
        #region replace html tag

/// <summary>
/// 移除Html标记
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static string RemoveHtml(string content) {
string regexstr = @"<[^>]*>";
return Regex.Replace(content, regexstr, string.Empty, RegexOptions.IgnoreCase);
}

/// <summary>
/// 过滤HTML中的不安全标签
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public static string RemoveUnsafeHtml(string content) {
content = Regex.Replace(content, @"(\<|\s+)o([a-z]+\s?=)", "$1$2", RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"(script|frame|form|meta|behavior|style)([\s|:|>])+", "$1.$2", RegexOptions.IgnoreCase);
return content;
}


public static string RemoveHTML(string strHtml) {
string[] aryReg ={
@"<script[^>]*?>.*?</script>",
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(file://[""'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"
};
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", "");

return strOutput;
}

public static string ClearSpecialHTMLTag(string strInput, string strPattern, string strReplace) {
Regex regex = new Regex(strPattern, RegexOptions.IgnoreCase);
return regex.Replace(strInput, strReplace);
}

#endregion

using System.Text.RegularExpressions;
q107770540 2010-10-18
  • 打赏
  • 举报
回复
using System.Text.RegularExpressions;
jce195447 2010-10-18
  • 打赏
  • 举报
回复
regex 是什么?如何引用
shaoliang520xi 2010-10-18
  • 打赏
  • 举报
回复
#region 过滤掉 html代码
/// <summary>
/// 过滤html标签
/// </summary>
/// <param name="strHtml">html的内容</param>
/// <returns></returns>
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);",
@"&(copy|#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++)
{
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(aryReg[i], System.Text.RegularExpressions.RegexOptions.IgnoreCase);
strOutput = regex.Replace(strOutput, aryRep[i]);
}
//strOutput.Replace("<", "");
//strOutput.Replace(">", "");
strOutput = strOutput.Replace("\r\n", "");
return strOutput;
}
#endregion
q107770540 2010-10-18
  • 打赏
  • 举报
回复

/// <summary>
/// 去掉html标记
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
protected static string ConvertGettext(string str)
{
Regex regex = new Regex(@"\<(.*?)\>", RegexOptions.IgnoreCase);

return regex.Replace(str, "").Replace(" ", "").Replace("\n", "").Replace("\r", "");
}


62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧