htmlencode() for a specific tags only
i have a long string dat can con开发者_JAVA技巧tain html tags applying htmlencode will encode all the tags but i want this method to leave some specific tags intact how is it possible
Encode the entire string, then decode the specific tags that you don't want encoded.
If you are allowing only simple tags without any attributes (like for example <b>
and <u>
), then you can decode them using a simple Replace
.
Assuming that your input HTML is well-formed, you can use a regular expression. This is possible because you are not trying to find matching pairs in nested tags, and you are not worried about tags that occur inside HTML comments. Otherwise a regular expression would be a bad candidate for this job.
var allowedTags = new[] { "a", "abbr", "br", /* etc. */ };
var output = Regex.Replace(input,
// Matches a single start or end tag
@"</?(\w+)[^>]*>",
// If the tag is one of the allowed tags...
me => allowedTags.Contains(me.Groups[1].Value)
// ... keep it unchanged
? me.Value
// otherwise, HTML-encode it
: HttpServerUtility.HtmlEncode(me.Value),
RegexOptions.Singleline);
If your HTML comes from users, then you cannot assume that it is well-formed. In such a case, I would recommend a more robust solution, e.g. using the Html Agility Pack.
精彩评论