Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decode Numeric HTML Entities in ColdFusion?

I need a way to transform numeric HTML entities into their plain-text character equivalent. For example, I would like to turn the entity:

é

into the character:

é

Through some googling around I found a function called HtmlUnEditFormat, but this function only transforms named entities. Is there a way to decode numeric entities in ColdFusion?

like image 838
pb. Avatar asked Oct 29 '09 21:10

pb.


Video Answer


1 Answers

Updated Answer:

Thanks to Todd Sharp for pointing out a very simple way to do this, using the Apache Commons StringEscapeUtils library, which is packaged with CF (and Railo), so you can just do:

<cfset Entity = "&##0233;" />
<cfset StrEscUtils = createObject("java", "org.apache.commons.lang.StringEscapeUtils") />
<cfset Character = StrEscUtils.unescapeHTML(Entity) />



Original Answer:

That linked function is icky - there's no need to name them explicitly, and as you say it doesn't do numerics.

Much simpler is to let CF do the work for you - using the XmlParse function:

<cffunction name="decodeHtmlEntity" returntype="String" output="false">
    <cfargument name="Entity" type="String" hint="&##<number>; or &<name>;" />
    <cfreturn XmlParse('<xml>#Arguments.Entity#</xml>').XmlRoot.XmlText />
</cffunction>

That one works with Railo, I can't remember if CF supports that syntax yet though, so you might need to change it to:

<cffunction name="decodeHtmlEntity" returntype="String" output="false">
    <cfargument name="Entity" type="String" hint="&##<number>; or &<name>;" />
    <cfset var XmlDoc = XmlParse('<xml>#Arguments.Entity#</xml>') />
    <cfreturn XmlDoc.XmlRoot.XmlText />
</cffunction>
like image 185
Peter Boughton Avatar answered Oct 24 '22 20:10

Peter Boughton