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;
}