在 ColdFusion 中解码数字 HTML 实体? [英] Decode Numeric HTML Entities in ColdFusion?

查看:17
本文介绍了在 ColdFusion 中解码数字 HTML 实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来将数字 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:

é

进入角色:

é

通过谷歌搜索,我发现了一个名为 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 指出了一种非常简单的方法,使用与 CF(和 Railo)一起打包的 Apache Commons StringEscapeUtils 库,因此您可以这样做:

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) />



那个链接的函数很讨厌 - 没有必要明确地命名它们,正如你所说的那样它不做数字.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆