- using System.Collections.Specialized;
- // ...
- class HTMLEntityManagement
- {
- //Declare a NameValue collection to hold the elements
- /// <summary>
- /// Removes HTML entities and replaces them with their actual character symbol
- /// </summary>
- /// <param name="input">HTML to be stripped</param>
- /// <returns>Stripped HTML containing character symbols instead of safe-elements</returns>
- public string stripEntities(String input)
- {
- for (int i = 0; i <= this.elementDictionary.Count - 1; i++)
- {
- string value = this.elementDictionary[i];
- string key = this.elementDictionary.Keys[i];
- input = input.Replace(key, value);
- }
- return input;
- }
- /// <summary>
- /// Replaces un-safe characters with HTML-safe entities
- /// </summary>
- /// <param name="input">HTML to be made safe</param>
- /// <returns>Safe HTML</returns>
- public string addEntities(String input)
- {
- for (int i = 0; i <= this.elementDictionary.Count - 1; i++)
- {
- string value = this.elementDictionary[i];
- string key = this.elementDictionary.Keys[i];
- input = input.Replace(value, key);
- }
- return input;
- }
- public HTMLEntityManagement()
- {
- /* Add any HTML elements, as so:
- *
- * Key: HTML element
- * Value: Character symbol
- */
- this.elementDictionary.Add(""", @"""");
- this.elementDictionary.Add("&", "&");
- this.elementDictionary.Add("'", "'"); //XML/XHTML ONLY
- this.elementDictionary.Add("<", "<");
- this.elementDictionary.Add(">", ">");
- this.elementDictionary.Add(" ", " ");
- this.elementDictionary.Add("¡p;", "¡");
- this.elementDictionary.Add("¢", "¢");
- this.elementDictionary.Add("£", "£");
- this.elementDictionary.Add("¤", "¤");
- this.elementDictionary.Add("¥", "¥");
- this.elementDictionary.Add("©", "©");
- this.elementDictionary.Add("®", "®");
- this.elementDictionary.Add("µ", "µ");
- this.elementDictionary.Add("º", "º");
- this.elementDictionary.Add("·", "·");
- //...
- }
- }
C#: HTML Safe Entity Management - http://www.glassoforange.co.uk/?p=7
