public static string EscapeHTMLTags(string source, List <string> excluded = null) { bool bInside = false; string curTag = ""; HTMLTag tagChecker = new HTMLTag(); string escaped = ""; for (int i = 0; i < source.Length; i++) { char let = source[i]; if (let == '<') { if (i < source.Length - 1) { char letNext = source[i + 1]; if (letNext != ' ') { curTag = "<"; bInside = true; continue; } } } else if (let == '>' && bInside) { curTag += ">"; if (!tagChecker.IsHTMLTag(curTag)) { bInside = false; continue; } Boolean bIsExcluded = excluded != null && (excluded.Contains(curTag) || excluded.Contains(curTag.ToUpper())); if (!bIsExcluded) { escaped += tagChecker.GetEscapedTag(curTag); } else { escaped += curTag; } bInside = false; continue; } if (!bInside) { escaped += let; } else { curTag += let; } } return(escaped); }
public static string StripHTMLTags(string source, List <string> excluded = null) { char[] array = new char[source.Length]; int arrayIndex = 0; bool bInside = false; string curTag = ""; HTMLTag tagChecker = new HTMLTag(); for (int i = 0; i < source.Length; i++) { char let = source[i]; if (let == '<') { if (i < source.Length - 1) { char letNext = source[i + 1]; if (letNext != ' ') { curTag = "<"; bInside = true; continue; } } } else if (let == '>' && bInside) { curTag += ">"; if (!tagChecker.IsHTMLTag(curTag)) { bInside = false; continue; } Boolean bIsExcluded = excluded != null && (excluded.Contains(curTag) || excluded.Contains(curTag.ToUpper())); if (bIsExcluded) { for (int j = 0; j < curTag.Length; j++) { array[arrayIndex] = curTag[j]; arrayIndex++; } } bInside = false; continue; } if (!bInside) { array[arrayIndex] = let; arrayIndex++; } else { curTag += let; } } string stripped = new string(array, 0, arrayIndex); stripped = stripped.Replace(" ", ""); return(stripped); }