Use C # code to clear unnecessary html

Source: Internet
Author: User

All company profiles in the database are saved from the text editor. Of course, the style is also saved to the database. What should I do if I only want to get the text content?

 

Code

  # Region Filter HTML
///   <Summary>
/// Filter HTML tags
///   </Summary>
///   <Param name = "strhtml"> HTML content </Param>
///   <Returns> </returns>
Public   Static   String Striphtml ( String Strhtml)
{
String [] Aryreg = {
@" <SCRIPT [^>] *?>. *? </SCRIPT> " ,

@" <(\/\ S *)?!? (\ W + :)? \ W +) (\ W + (\ s * =? \ S * (["" ']) (\ ["" 'tbnr] | [^ \ 7]) *? \ 7 | \ W +) |. {0}) | \ s )*? (\/\ S *)?> " ,
@" ([\ R \ n]) [\ s] + " ,
@" & (Quot | #34 ); " ,
@" & (Amp | #38 ); " ,
@" & (LT | #60 ); " ,
@" & (GT | #62 ); " ,
@" & Amp; (nbsp | #160 ); " ,
@" & (Iexcl | #161 ); " ,
@" & (Cent | #162 ); " ,
@" & (Pound | #163 ); " ,
@" & (Copy | #169 ); " ,
@" & # (\ D + ); " ,
@" --> " ,
@" <! --. * \ N "
};

String [] Aryrep = {
"" ,
"" ,
"" ,
" \ "" ,
" & " ,
" < " ,
" > " ,
"   " ,
" \ XA1 " , // CHR (1, 161 ),
" \ Xa2 " , // CHR (1, 162 ),
" \ Xa3 " , // CHR (1, 163 ),
" \ Xa9 " , // CHR (1, 169 ),
"" ,
" \ R \ n " ,
""
};

String Newreg = Aryreg [ 0 ];
String Stroutput = Strhtml;
For ( Int I =   0 ; I < Aryreg. length; I ++ )
{
System. Text. regularexpressions. RegEx =   New System. Text. regularexpressions. RegEx (aryreg [I], system. Text. regularexpressions. regexoptions. ignorecase );
Stroutput = RegEx. Replace (stroutput, aryrep [I]);
}
Stroutput. Replace ( " < " , "" );
Stroutput. Replace ( " > " , "" );
Stroutput. Replace ( " \ R \ n " , "" );
Return Stroutput;
}
# Endregion

 

 

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.