{"id":169,"date":"2011-05-09T20:09:35","date_gmt":"2011-05-09T18:09:35","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=169"},"modified":"2011-05-09T20:18:49","modified_gmt":"2011-05-09T18:18:49","slug":"escaping-text-in-html","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2011\/05\/escaping-text-in-html\/","title":{"rendered":"Escaping text in HTML"},"content":{"rendered":"<p>Thought I just share this little function on its own. I created a simple method that escape (convert) a string to make it &#8216;safe&#8217; for html display.<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">public static string<\/span> EscapeText(<span style=\"color: #0000ff;\">string <\/span>text)<br \/>\n{<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">string <\/span>escapeChars2 = @&#8221;<span style=\"color: #800000;\">&lt;&gt;`\u00b4\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5\u00e8\u00e9\u00ea\u00eb\u00ec\u00ed\u00ee\u00ef\u00f1\u00f2\u00f3\u00f4\u00f5\u00f6\u00f7\u00f8\u00f9\u00fa\u00fb\u00fc\u00fd\u00c0\u00c1\u00c2\u00c3\u00c4\u00c5\u00c8\u00c9\u00ca\u00cb\u00cc\u00cd\u00ce\u00cf\u00d1\u00d2\u00d3\u00d4\u00d5\u00d6\u00d7\u00d8\u00d9\u00da\u00db\u00dc\u00dd<\/span>&#8220;;<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">if <\/span>(<span style=\"color: #0000ff;\">string<\/span>.IsNullOrEmpty(text))<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">return <\/span>&#8220;&#8221;;<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">else if<\/span> (text.IndexOfAny(escapeChars2.ToCharArray()) == -1)<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">return <\/span>text;<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">else<\/span><br \/>\n{<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">return <\/span>text<br \/>\n.Replace(&#8220;&lt;&#8220;, &#8220;&amp;lt;&#8221;)<br \/>\n.Replace(&#8220;&gt;&#8221;, &#8220;&amp;gt;&#8221;)<br \/>\n.Replace(&#8220;&amp;&#8221;, &#8220;&amp;amp;&#8221;)<br \/>\n.Replace(&#8220;`&#8221;, &#8220;&amp;#96;&#8221;)<br \/>\n.Replace(&#8220;\u00b4&#8221;, &#8220;&amp;acute;&#8221;)<br \/>\n.Replace(&#8220;\u00e0&#8221;, &#8220;&amp;agrave;&#8221;)<br \/>\n.Replace(&#8220;\u00e1&#8221;, &#8220;&amp;aacute;&#8221;)<br \/>\n.Replace(&#8220;\u00e2&#8221;, &#8220;&amp;acirc;&#8221;)<br \/>\n.Replace(&#8220;\u00e3&#8221;, &#8220;&amp;atilde;&#8221;)<br \/>\n.Replace(&#8220;\u00e4&#8221;, &#8220;&amp;auml;&#8221;)<br \/>\n.Replace(&#8220;\u00e5&#8221;, &#8220;&amp;aring;&#8221;)<br \/>\n.Replace(&#8220;\u00e8&#8221;, &#8220;&amp;egrave;&#8221;)<br \/>\n.Replace(&#8220;\u00e9&#8221;, &#8220;&amp;eacute;&#8221;)<br \/>\n.Replace(&#8220;\u00ea&#8221;, &#8220;&amp;ecirc;&#8221;)<br \/>\n.Replace(&#8220;\u00eb&#8221;, &#8220;&amp;euml;&#8221;)<br \/>\n.Replace(&#8220;\u00ec&#8221;, &#8220;&amp;igrave;&#8221;)<br \/>\n.Replace(&#8220;\u00ed&#8221;, &#8220;&amp;iacute;&#8221;)<br \/>\n.Replace(&#8220;\u00ee&#8221;, &#8220;&amp;icirc;&#8221;)<br \/>\n.Replace(&#8220;\u00ef&#8221;, &#8220;&amp;iuml;&#8221;)<br \/>\n.Replace(&#8220;\u00f1&#8221;, &#8220;&amp;ntilde;&#8221;)<br \/>\n.Replace(&#8220;\u00f2&#8221;, &#8220;&amp;ograve;&#8221;)<br \/>\n.Replace(&#8220;\u00f3&#8221;, &#8220;&amp;oacute;&#8221;)<br \/>\n.Replace(&#8220;\u00f4&#8221;, &#8220;&amp;ocirc;&#8221;)<br \/>\n.Replace(&#8220;\u00f5&#8221;, &#8220;&amp;otilde;&#8221;)<br \/>\n.Replace(&#8220;\u00f6&#8221;, &#8220;&amp;ouml;&#8221;)<br \/>\n.Replace(&#8220;\u00f7&#8221;, &#8220;&amp;divide;&#8221;)<br \/>\n.Replace(&#8220;\u00f8&#8221;, &#8220;&amp;oslash;&#8221;)<br \/>\n.Replace(&#8220;\u00f9&#8221;, &#8220;&amp;ugrave;&#8221;)<br \/>\n.Replace(&#8220;\u00fa&#8221;, &#8220;&amp;uacute;&#8221;)<br \/>\n.Replace(&#8220;\u00fb&#8221;, &#8220;&amp;ucirc;&#8221;)<br \/>\n.Replace(&#8220;\u00fc&#8221;, &#8220;&amp;uuml;&#8221;)<br \/>\n.Replace(&#8220;\u00fd&#8221;, &#8220;&amp;yacute;&#8221;)<br \/>\n.Replace(&#8220;\u00ff&#8221;, &#8220;&amp;#255;&#8221;)<br \/>\n.Replace(&#8220;\u00c0&#8221;, &#8220;&amp;Agrave;&#8221;)<br \/>\n.Replace(&#8220;\u00c1&#8221;, &#8220;&amp;Aacute;&#8221;)<br \/>\n.Replace(&#8220;\u00c2&#8221;, &#8220;&amp;Acirc;&#8221;)<br \/>\n.Replace(&#8220;\u00c3&#8221;, &#8220;&amp;Atilde;&#8221;)<br \/>\n.Replace(&#8220;\u00c4&#8221;, &#8220;&amp;Auml;&#8221;)<br \/>\n.Replace(&#8220;\u00c5&#8221;, &#8220;&amp;Aring;&#8221;)<br \/>\n.Replace(&#8220;\u00c8&#8221;, &#8220;&amp;Egrave;&#8221;)<br \/>\n.Replace(&#8220;\u00c9&#8221;, &#8220;&amp;Eacute;&#8221;)<br \/>\n.Replace(&#8220;\u00ca&#8221;, &#8220;&amp;Ecirc;&#8221;)<br \/>\n.Replace(&#8220;\u00cb&#8221;, &#8220;&amp;Euml;&#8221;)<br \/>\n.Replace(&#8220;\u00cc&#8221;, &#8220;&amp;Igrave;&#8221;)<br \/>\n.Replace(&#8220;\u00cd&#8221;, &#8220;&amp;Iacute;&#8221;)<br \/>\n.Replace(&#8220;\u00ce&#8221;, &#8220;&amp;Icirc;&#8221;)<br \/>\n.Replace(&#8220;\u00cf&#8221;, &#8220;&amp;Iuml;&#8221;)<br \/>\n.Replace(&#8220;\u00d1&#8221;, &#8220;&amp;Ntilde;&#8221;)<br \/>\n.Replace(&#8220;\u00d2&#8221;, &#8220;&amp;Ograve;&#8221;)<br \/>\n.Replace(&#8220;\u00d3&#8221;, &#8220;&amp;Oacute;&#8221;)<br \/>\n.Replace(&#8220;\u00d4&#8221;, &#8220;&amp;Ocirc;&#8221;)<br \/>\n.Replace(&#8220;\u00d5&#8221;, &#8220;&amp;Otilde;&#8221;)<br \/>\n.Replace(&#8220;\u00d6&#8221;, &#8220;&amp;Ouml;&#8221;)<br \/>\n.Replace(&#8220;\u00d7&#8221;, &#8220;&amp;times;&#8221;)<br \/>\n.Replace(&#8220;\u00d8&#8221;, &#8220;&amp;Oslash;&#8221;)<br \/>\n.Replace(&#8220;\u00d9&#8221;, &#8220;&amp;Ugrave;&#8221;)<br \/>\n.Replace(&#8220;\u00da&#8221;, &#8220;&amp;Uacute;&#8221;)<br \/>\n.Replace(&#8220;\u00db&#8221;, &#8220;&amp;Ucirc;&#8221;)<br \/>\n.Replace(&#8220;\u00dc&#8221;, &#8220;&amp;Uuml;&#8221;)<br \/>\n.Replace(&#8220;\u00dd&#8221;, &#8220;&amp;Yacute;&#8221;);<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>}<\/p><\/blockquote>\n<p>There are other ways to do the string matching &#8211; like Regular Expressions, but they add some other overhead for a method that does something simple like this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thought I just share this little function on its own. I created a simple method that escape (convert) a string to make it &#8216;safe&#8217; for html display. public static string EscapeText(string text) { string escapeChars2 = @&#8221;&lt;&gt;`\u00b4\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5\u00e8\u00e9\u00ea\u00eb\u00ec\u00ed\u00ee\u00ef\u00f1\u00f2\u00f3\u00f4\u00f5\u00f6\u00f7\u00f8\u00f9\u00fa\u00fb\u00fc\u00fd\u00c0\u00c1\u00c2\u00c3\u00c4\u00c5\u00c8\u00c9\u00ca\u00cb\u00cc\u00cd\u00ce\u00cf\u00d1\u00d2\u00d3\u00d4\u00d5\u00d6\u00d7\u00d8\u00d9\u00da\u00db\u00dc\u00dd&#8220;; if (string.IsNullOrEmpty(text)) return &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2011\/05\/escaping-text-in-html\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,8,9,303],"class_list":["post-169","post","type-post","status-publish","format-standard","hentry","category-development","tag-net","tag-c","tag-code","tag-development"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":6,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":174,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/169\/revisions\/174"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}