Filter HTML string summary in ASP. NET, asp.net string
Write down for backup first!
C # code
- /// <Summary> remove HTML tags
- ///
- /// </Summary>
- /// <Param name = "Htmlstring"> including the source code of HTML </param>
- /// <Returns> removed text </returns>
- Public static string GetNoHTMLString (string Htmlstring)
- {
- // Delete the script
- Htmlstring = Regex. Replace (Htmlstring, @ "<script [^>] *?>. *? </Script> "," ", RegexOptions. IgnoreCase );
- // Delete 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 = HttpContext. Current. Server. HtmlEncode (Htmlstring). Trim ();
- Return Htmlstring;
- }
- /// <Summary> obtain the displayed string to display HTML tags, but filter dangerous HTML tags, such as iframe and script.
- ///
- /// </Summary>
- /// <Param name = "str"> unprocessed string </param>
- /// <Returns> </returns>
- Public static string GetSafeHTMLString (string str)
- {
- Str = Regex. Replace (str, @ "<applet [^>] *?>. *? </Applet> "," ", RegexOptions. IgnoreCase );
- Str = Regex. Replace (str, @ "<body [^>] *?>. *? </Body> "," ", RegexOptions. IgnoreCase );
- Str = Regex. Replace (str, @ "<embed [^>] *?>. *? </Embed> "," ", RegexOptions. IgnoreCase );
- Str = Regex. Replace (str, @ "<frame [^>] *?>. *? </Frame> "," ", RegexOptions. IgnoreCase );
- Str = Regex. Replace (str, @ "<script [^>] *?>. *? </Script> "," ", RegexOptions. IgnoreCase );
- Str = Regex. Replace (str, @ "<frameset [^>] *?>. *? </Frameset> "," ", RegexOptions. IgnoreCase );
- Str = Regex. Replace (str, @ "
- Str = Regex. Replace (str, @ "<iframe [^>] *?>. *? </Iframe> "," ", RegexOptions. IgnoreCase );
- Str = Regex. Replace (str, @ "<style [^>] *?>. *? </Style> "," ", RegexOptions. IgnoreCase );
- Str = Regex. Replace (str, @ "<layer [^>] *?>. *? </Layer> "," ", RegexOptions. IgnoreCase );
- Str = Regex. Replace (str, @ "<link [^>] *?>. *? </Link> "," ", RegexOptions. IgnoreCase );
- Str = Regex. Replace (str, @ "<ilayer [^>] *?>. *? </Ilayer> "," ", RegexOptions. IgnoreCase );
- Str = Regex. Replace (str, @ "<meta [^>] *?>. *? </Meta> "," ", RegexOptions. IgnoreCase );
- Str = Regex. Replace (str, @ "<object [^>] *?>. *? </Object> "," ", RegexOptions. IgnoreCase );
- Return str;
- }
How does aspnet filter out html code?
Public static string NoHtml (string text)
{
// Delete the script
Text = Regex. Replace (text, @ "<script [^>] *?>. *? </Script> "," ", RegexOptions. IgnoreCase );
// Delete HTML
Text = Regex. Replace (text, @ "<(. [^>] *)>", "", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "([\ r \ n]) [\ s] +", "", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "-->", "", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "<! --. * "," ", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "& (quot | #34);", "\" ", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "& (amp | #38);", "&", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "& (lt | #60);", "<", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "& (gt | #62);", ">", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "& (nbsp | #160);", "", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "& (iexcl | #161);", "\ xa1", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "& (cent | #162);", "\ xa2", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "& (poun ...... the remaining full text>
Asp net filters html code
By default, POST requests containing HTML tags are prohibited. Set ValidateRequest = "false ".
For example:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = YourNameSpace. YourClassName "ValidateRequest =" false "%>