Myself from the Internet to find a way to filter HTML tags, I do not know who is the original, anyway, a lot of the same. I copied the method, the code is as follows:
/// <summary>///Remove HTML Tags/// </summary>/// <param name= "nohtml" >including the HTML source code</param>/// <returns>text that has been removed</returns> Public Static stringNohtml (stringhtmlstring) { //Delete Scripthtmlstring = Regex.Replace (htmlstring,@"<script[^>]*?>.*?</script>","", regexoptions.ignorecase); //Delete HTMLhtmlstring = 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 (); returnhtmlstring;}
The above code is directly copied from the Internet, this really can filter out all the HTML tags, but this is not what I want, this filter is too clean, if I use the textarea input box, I want to keep the space with the line.
And then I changed it myself. This method, textarea the line is \ n, so I have to replace these tags with <br>, so that from the database read to the page, you can correct the line, the space is replaced by the HTML whitespace, done.
/// <summary> ///Remove HTML tags (keep br and \ r \ n) (this method is modified by the blog Park-Three scrolls)/// </summary> /// <param name= "nohtml" >including the HTML source code</param> /// <returns>text that has been removed</returns> Public Static stringNewnohtml (stringhtmlstring) { //htmlstring.replace ("\\r\\n", "%r%n"). Replace ("<br>", "%br%"). Replace ("<br/>", "%br&%"). Replace ("\\n", "%n"); //Delete Scripthtmlstring = Regex.Replace (htmlstring,@"<script[^>]*?>.*?</script>","", regexoptions.ignorecase); //Delete HTMLhtmlstring = 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)"," "); returnhtmlstring; }
This filter can be used to allow users to enter the content of the filter, if there are shortcomings, please criticize correct!
ASP. NET filters HTML tags, leaving only line breaks and spaces