Use ColdFusion to Revert HTMLEditFormat()

This ColdFusion function is a simple solution I saw somewhere on the Web a while back. I needed it yesterday, but my rather rusty brain forgot where it was. And the Web is too big, and the page too small, for Google to locate it. I’m happy to say I remembered a bit of it, so I simply recreated it.

HTMLEditFormat() can convert special characters and symbols, such as & or ©, into HTML special character codes, like & or ©. However, there is no function to convert those special character codes back to normal characters. At least, there wasn’t one before––here is a simple function that can do it.

   1: <cffunction name="HTMLStringFormat" access="public" output="No" >
   2:  <cfargument name="string" type="string" required="Yes" >
   3:  <cfset local.special = "&ndash;,&mdash;,&iexcl;,&iquest;,&quot;,&ldquo;,&rdquo;,&lsquo;,&rsquo;,&laquo;,&raquo;,&nbsp;,&amp;,&cent;,&copy;,&divide;,&gt;,&lt;,&micro;,&middot;,&para;,&plusmn;,&euro;,&pound;,&reg;,&sect;,&trade;,&yen;,&aacute;,&Aacute;,&agrave;,&Agrave;,&acirc;,&Acirc;,&aring;,&Aring;,&atilde;,&Atilde;,&auml;,&Auml;,&aelig;,&AElig;,&ccedil;,&Ccedil;,&eacute;,&Eacute;,&egrave;,&Egrave;,&ecirc;,&Ecirc;,&euml;,&Euml;,&iacute;,&Iacute;,&igrave;,&Igrave;,&icirc;,&Icirc;,&iuml;,&Iuml;,&ntilde;,&Ntilde;,&oacute;,&Oacute;,&ograve;,&Ograve;,&ocirc;,&Ocirc;,&oslash;,&Oslash;,&otilde;,&Otilde;,&ouml;,&Ouml;,&szlig;,&uacute;,&Uacute;,&ugrave;,&Ugrave;,&ucirc;,&Ucirc;,&uuml;,&Uuml;,&yuml;">
   4:  <cfset local.normal = "#chr(8211)#,#chr(8212)#,#chr(161)#,#chr(191)#,#chr(34)#,#chr(8220)#,#chr(8221)#,#chr(39)#,#chr(39)#,#chr(171)#,#chr(187)#,#chr(32)#,#chr(38)#,#chr(162)#,#chr(169)#,#chr(247)#,#chr(62)#,#chr(60)#,#chr(181)#,#chr(183)#,#chr(182)#,#chr(177)#,#chr(8364)#,#chr(163)#,#chr(174)#,#chr(167)#,#chr(8482)#,#chr(165)#,#chr(225)#,#chr(193)#,#chr(224)#,#chr(192)#,#chr(226)#,#chr(194)#,#chr(229)#,#chr(197)#,#chr(227)#,#chr(195)#,#chr(228)#,#chr(196)#,#chr(230)#,#chr(198)#,#chr(231)#,#chr(199)#,#chr(233)#,#chr(201)#,#chr(232)#,#chr(200)#,#chr(234)#,#chr(202)#,#chr(235)#,#chr(203)#,#chr(237)#,#chr(205)#,#chr(236)#,#chr(204)#,#chr(238)#,#chr(206)#,#chr(239)#,#chr(207)#,#chr(241)#,#chr(209)#,#chr(243)#,#chr(211)#,#chr(242)#,#chr(210)#,#chr(244)#,#chr(212)#,#chr(248)#,#chr(216)#,#chr(245)#,#chr(213)#,#chr(246)#,#chr(214)#,#chr(223)#,#chr(250)#,#chr(218)#,#chr(249)#,#chr(217)#,#chr(251)#,#chr(219)#,#chr(252)#,#chr(220)#,#chr(255)#">
   5:  <cfset local.formated = ReplaceList(arguments.string, local.special, local.normal)>
   6:  <cfreturn local.formated>
   7: </cffunction>
Show/Hide Line Numbers . Full Screen . Plain

#HTMLStringFormat('Marks &amp; Spencer')# will return Marks & Spencer.

Name  
(required)
Email  
(required - never shown publicly)
Web Site  
Notify me of new comments via email.
Notify me of replies via email.