public static string HtmlEncode (String thestring)
{
TheString = Thestring.replace (">", ">");
TheString = Thestring.replace ("<", "<");
TheString = Thestring.replace ("", " ");
TheString = Thestring.replace ("", " ");
TheString = Thestring.replace ("\" "," " ");
TheString = Thestring.replace ("\" "," & #39; ");
TheString = Thestring.replace ("\ n", "<br/>");
return thestring;
}
The first paragraph is the conversion of textarea inside the special characters, the use is: for example, when doing a Web page, provide a textarea box to the user, let the user enter a self-introduction of what, and then save to the database, in the library before saving to do such a conversion, for security reasons, There is a special character so that after the conversion to retain, or in the textarea to knock the space and enter, in the save time, these are not, output to the web will find, what format is not.
public static string Htmldiscode (String thestring)
{
TheString = Thestring.replace (">", ">");
TheString = Thestring.replace ("<", "<");
TheString = Thestring.replace (" ", "");
TheString = Thestring.replace (" ", "");
TheString = Thestring.replace (""", "\" ");
TheString = Thestring.replace ("& #39;", "\");
TheString = Thestring.replace ("<br/>", "\ n");
return thestring;
}
This paragraph of code is exactly the same as the first paragraph of code, for example, after the user login, to modify the profile, you want to put your saved personal profile into the textarea, you must do a reverse conversion, otherwise it will show some characters that are not user input.
public static string dealhtml (String str)
{
str = regex.replace (str, @ "\<" (IMG) [^>]*>|<\/(IMG) > "," ", regexoptions.ignorecase);
str = regex.replace (str, @ "\< (table|tbody|tr|td|th|) [^>]*>|<\/(table|tbody|tr|td|th|) > "," ", regexoptions.ignorecase);
str = regex.replace (str, @ "\< (div|blockquote|fieldset|legend) [^>]*>|<\/(div|blockquote|fieldset| Legend) > "," ", regexoptions.ignorecase);
str = regex.replace (str, @ "\< (font|i|u|h[1-9]|s) [^>]*>|<\/(Font|i|u|h[1-9]|s) >", "", Regexoptions.ignorecase);
str = regex.replace (str, @ "\< (style|strong) [^>]*>|<\/(Style|strong) >", "", regexoptions.ignorecase);
str = regex.replace (str, @ "\<a[^>]*>|<\/a>", "", regexoptions.ignorecase);
str = regex.replace (str, @ "\< (meta|iframe|frame|span|tbody|layer) [^>]*>|<\/(iframe|frame|meta|span| Tbody|layer) > "," ", regexoptions.ignorecase);
str = regex.replace (str, @ "\<a[^>]*", "", regexoptions.ignorecase);
Return St
This code filters HTML tags with regular expressions, and the HTML tags it mentions are filtered out
For example, when publishing an article, using an HTML online editor, so that the content of the article contains HTML code, if this time, you want to display a portion of the content on the homepage of the site, if you directly intercept the content of the article, may be included in the HTML code truncation, So the HTML code displayed on the home page is not complete, will lead to some broken HTML code, our general practice is to filter out HTML code, only the text, so that the display and then use CSS format, on the home page, it looks much more, this function, is to filter str into pure text, That does not contain HTML
Replace, restore special characters in HTML