解码ColdFusion中的数字HTML实体? [英] Decode Numeric HTML Entities in ColdFusion?
问题描述
我需要一种方法来将数字HTML实体转换为纯文本字符等效项。例如,我要转换实体:
I need a way to transform numeric HTML entities into their plain-text character equivalent. For example, I would like to turn the entity:
é
键入字符:
é
通过一些googling我发现一个函数 HtmlUnEditFormat ,但此函数仅转换命名实体。有没有办法解码ColdFusion中的数字实体?
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?
推荐答案
更新答案:
感谢Todd Sharp指出了一个非常简单的方法来使用Apache Commons StringEscapeUtils库,它与CF(和Railo)一起打包,因此您可以执行:
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) />
这个链接函数是icky - 没有必要明确命名它们,因为你说它不做数字。
That linked function is icky - there's no need to name them explicitly, and as you say it doesn't do numerics.
更简单的是让CF为你工作 - 使用 XmlParse
函数:
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>
这一个与Railo一起工作,但我不记得CF是否支持该语法,可能需要将其更改为:
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>
这篇关于解码ColdFusion中的数字HTML实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!