References :
string myHtml = "<table><tr><td>my table</td></tr></table>"; string myText = Utils.String.StripTags(myHtml);
public static string StripTags(string pTaggedText) { return StripTags(pTaggedText, new string[] { }); } public static string StripTags(string pTaggedText, string[] pTagsToStrip) { if (pTagsToStrip.Length == 0) //strip all tags { Regex rx = new Regex("<[^>]+>"); string resultText = rx.Replace(pTaggedText, ""); return resultText; } else //strip only specified tags { string tagsToStrip = ""; for (int s = 0; s < pTagsToStrip.Length; s++) { if (s > 0) { tagsToStrip += "|"; } tagsToStrip += pTagsToStrip[s]; } Regex rx = new Regex("</?(?i:" + tagsToStrip + ")([^>]*>"); string resultText = rx.Replace(pTaggedText, ""); return resultText; } }
I have not worked enough on the above function, that I am confident it is reliable. I would very much appreciate comments on how to improve the function. One way to improve the function is to improve the regular expression used, currently I have these in mind :
If you have a computer, you are managing links
Manylink allows you to :