Asp. NET Filter HTML string method summary

Source: Internet
Author: User
This article describes the method of ASP. NET Filter HTML string, for everyone to refer to the use, the specific code is as follows:

<summary> Remove HTML tags//////</summary>//<param name= "htmlstring" > include HTML source </param> <returns> removed text </returns> public static string getnohtmlstring (String htmlstring) {//delete script Htm   lstring = 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 displays HTML tags, but filters out dangerous HTML tags, such as iframe,script. </summary>//<param name= "str" > UnhandledString </param>///<returns></returns> public static string getsafehtmlstring (String str) {str = REGEX.R   Eplace (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, @ "
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.