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 = "–,—,¡,¿,",“,”,‘,’,«,», ,&,¢,©,÷,>,<,µ,·,¶,±,€,£,®,§,™,¥,á,Á,à,À,â,Â,å,Å,ã,Ã,ä,Ä,æ,Æ,ç,Ç,é,É,è,È,ê,Ê,ë,Ë,í,Í,ì,Ì,î,Î,ï,Ï,ñ,Ñ,ó,Ó,ò,Ò,ô,Ô,ø,Ø,õ,Õ,ö,Ö,ß,ú,Ú,ù,Ù,û,Û,ü,Ü,ÿ"> 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> #HTMLStringFormat('
Marks & Spencer')# will return
Marks & Spencer.
Posted by Saman W Jayasekara at Thursday 09 September 2010 11:55 AM .
ColdFusion