<summary>
Remove HTML tags
</summary>
<param name= "htmlstring" > including HTML source code </param>
<returns> has been removed after the text </returns>
public static string Getnohtmlstring (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, @ "-and", "", 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>
Gets the displayed string, which can display HTML tags, but filters out dangerous HTML tags, such as iframe,script.
</summary>
<param name= "str" > Unhandled 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;
}
ASP. NET string filtering