Asp. NET filter HTML tags only to keep line and space method _ practical skills

Source: Internet
Author: User
Tags html tags httpcontext

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.

Related Article

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.