ASP. NET filters HTML tags, leaving only line breaks and spaces

Source: Internet
Author: User
Tags httpcontext

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)","&nbsp;"); 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.