This article mainly introduced the ASP. NET filter HTML tag only preserves the line wrapping and the space method, contains the network common method as well as the improvement to this method, has the certain reference value, the need friend can refer to the next
The example in this article describes how ASP. NET Filter HTML tags retain only line breaks and whitespace. Share to everyone for your reference. The specific analysis is as follows:
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)/// </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 filter when publishing content.
I hope this article is helpful to everyone's ASP.
ASP. NET filter HTML tags only preserves line wrapping and whitespace methods