Replace, restore special characters in HTML

Source: Internet
Author: User

public static string HtmlEncode (String thestring)
{
TheString = Thestring.replace (">", ">");
TheString = Thestring.replace ("<", "&lt;");
TheString = Thestring.replace ("", "&nbsp;");
TheString = Thestring.replace ("", "&nbsp;");
TheString = Thestring.replace ("\" "," &quot; ");
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 ("&gt;", ">");
TheString = Thestring.replace ("&lt;", "<");
TheString = Thestring.replace ("&nbsp;", "");
TheString = Thestring.replace ("&nbsp;", "");
TheString = Thestring.replace ("&quot;", "\" ");
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

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.