Special character filtering function added to my favorites

Source: Internet
Author: User

 

I have been using several functions here and I will post them below.
We hope to try to use the instance instead of simply using it, without worrying about its original meaning and Implementation ideas. Come on!

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Text. RegularExpressions;

/// <Summary>
/// Check and replace the special characters in the string
/// </Summary>
/// <Param name = "strHtml"> </param>
/// <Returns> </returns>
Public static string HtmlToTxt (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 );",
@ "& (Nbsp | #160 );",
@ "& (Iexcl | #161 );",
@ "& (Cent | #162 );",
@ "& (Pound | #163 );",
@ "& (Copy | #169 );",
@ "(\ D + );",
@ "--> ",
@ "<! --. * \ N"
};

String newReg = aryReg [0];
String strOutput = strHtml;
For (int I = 0; I <aryReg. Length; I ++)
{
Regex regex = new Regex (aryReg [I], RegexOptions. IgnoreCase );
StrOutput = regex. Replace (strOutput, string. Empty );
}

StrOutput. Replace ("<","");
StrOutput. Replace ("> ","");
StrOutput. Replace ("\ r \ n ","");

Return strOutput;
}

======================= Other two functions ============================ =
/// <Summary>
/// Replace special characters in html
/// </Summary>
/// <Param name = "theString"> the text to be replaced. </Param>
/// <Returns> the replaced text. </Returns>
Public string HtmlEncode (string theString)
{
TheString = theString. Replace (">", "& gt ;");
TheString = theString. Replace ("<", "& lt ;");
TheString = theString. Replace ("", "& nbsp ;");
TheString = theString. Replace ("", "& nbsp ;");
TheString = theString. Replace ("\" "," & quot ;");
TheString = theString. Replace ("\'","'");
TheString = theString. Replace ("\ n", "<br/> ");
Return theString;
}

/// <Summary>
/// Restore special characters in html
/// </Summary>
/// <Param name = "theString"> the text to be restored. </Param>
/// <Returns> restored text. </Returns>
Public string HtmlDiscode (string theString)
{
TheString = theString. Replace ("& gt;", "> ");
TheString = theString. Replace ("& lt;", "<");
TheString = theString. Replace ("& nbsp ;","");
TheString = theString. Replace ("& nbsp ;","");
TheString = theString. Replace ("& quot ;","\"");
TheString = theString. Replace ("'","\'");
TheString = theString. Replace ("<br/>", "\ n ");
Return theString;
}

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.