aspnet過濾html標簽
『壹』 求一過慮字元串中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; } 經測試可用 最終效果