The example in this article tells you how ASP.net filters HTML tags to keep only wrapping and spaces. Share to everyone for your reference. The specific analysis is as follows:
I found a way to filter HTML tags from the Internet, I do not know who is the original, anyway, a lot of the same. I copied the method and the code was as follows:
Copy Code code as follows:
<summary>
Remove HTML tags
</summary>
<param name= "nohtml" > including HTML source </param>
<returns> has been removed after the text </returns>
public static string nohtml (String htmlstring)
{
Delete 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;
}
The above code is copied directly from the Internet, this can really filter out all the HTML tags, but this is not what I want, this filter is too clean, if I use textarea input box, I want to keep the space and line.
Then I changed this method on my own, textarea line is \ n, so I have to replace these tags with <br>, so that when you read the page from the database, you can correct the line, replace the space into HTML spaces, done.
Copy Code code as follows:
<summary>
Remove HTML tags (keep br with \ r \ n)
</summary>
<param name= "nohtml" > including HTML source </param>
<returns> has been removed after the text </returns>
public static string newnohtml (String htmlstring)
{
Htmlstring.replace ("\\r\\n", "%r%n"). Replace ("<br>", "%br%"). Replace ("<br/>", "%br&%"). Replace ("\\n", "%n");
Delete Script
htmlstring = Regex.Replace (htmlstring, @ "<script[^>]*?>.*?</script>", "",
Regexoptions.ignorecase);
Delete HTML
htmlstring = Regex.Replace (htmlstring, @) < (. [ ^>]*) > "," ",
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);
htmlstring = Regex.Replace (htmlstring, @ "((\ r \ n))", "<br>");
htmlstring = Regex.Replace (htmlstring, @ "(\r|\n)", "<br>");
htmlstring = Regex.Replace (htmlstring, @ "(\s)", "");
return htmlstring;
}
This filter can be used to allow users to enter the filter when publishing content.
I hope this article will help you with the ASP.net program design.