C # filter HTML code

Source: Internet
Author: User
// Filter HTMLCode
Public   String Checkstr ( String Html)
{
System. Text. regularexpressions. RegEx regex1 =   New System. Text. regularexpressions. RegEx ( @" <SCRIPT [\ s] + </script *> " , System. Text. regularexpressions. regexoptions. ignorecase );
System. Text. regularexpressions. RegEx regex2 =   New System. Text. regularexpressions. RegEx ( @" Href * = * [\ s] * script *: " , System. Text. regularexpressions. regexoptions. ignorecase );
System. Text. regularexpressions. RegEx regex3 =   New System. Text. regularexpressions. RegEx ( @" On [\ s] * = " , System. Text. regularexpressions. regexoptions. ignorecase );
System. Text. regularexpressions. RegEx regex4 =   New System. Text. regularexpressions. RegEx ( @" <IFRAME [\ s] + </iframe *> " , System. Text. regularexpressions. regexoptions. ignorecase );
System. Text. regularexpressions. RegEx regex5 =   New System. Text. regularexpressions. RegEx ( @" <Frameset [\ s] + </frameset *> " , System. Text. regularexpressions. regexoptions. ignorecase );
System. Text. regularexpressions. RegEx regex6 =   New System. Text. regularexpressions. RegEx ( @" \ ] + \> " , System. Text. regularexpressions. regexoptions. ignorecase );
System. Text. regularexpressions. RegEx regex7 =   New System. Text. regularexpressions. RegEx ( @" </P> " , System. Text. regularexpressions. regexoptions. ignorecase );
System. Text. regularexpressions. RegEx regex8 =   New System. Text. regularexpressions. RegEx ( @" <P> " , System. Text. regularexpressions. regexoptions. ignorecase );

Html = Regex1.replace (HTML, "" ); // Filter <SCRIPT> </SCRIPT> tags
Html = Regex2.replace (HTML, "" ); // Filter href = javascript: (<A>) attributes
Html = Regex3.replace (HTML, " _ Disibledevent = " ); // Filter on events of other controls
Html = Regex4.replace (HTML, "" ); // Filter IFRAME
Html = Regex5.replace (HTML, "" ); // Filter frameset
Html = Regex6.replace (HTML, "" ); // Filter frameset
Html = Regex7.replace (HTML, "" ); // Filter frameset
Html = Regex8.replace (HTML, "" ); // Filter frameset
Html = Html. Replace ( "   " , "" );
Html = Html. Replace ( " </Strong> " , "" );
Html = Html. Replace ( " <Strong> " , "" );
Return HTML;
}

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.