The other week I was reading Ben’s Kinky Solution for removing high characters in ColdFusion strings. It’s an unfortunate fact that ColdFusion’s own tags do not work very well with the conversion of ASCII high characters. So with this in mind and inspired by the conversation in Ben’s post I decided to create a simple solution that was not dependent on external Java objects.
This function will scan through your string, find any characters that have a higher ASCII numeric value then 127 and automatically convert them to a hexadecimal numeric character. Which is then usable in any XML or XHTML document.
I have put a working example with code on my Civbox.com website, and this is a direct link to the code.
The example code can be found below.
<!----- XMLHighSafe.cfc 1.0 by civBox Software (http://www.civbox.com) : 11 March 2008 :
Released under the Creative Commons BSD License (http://creativecommons.org/licenses/BSD/) ----->
<cfcomponent output="no">
<cffunction name="XMLHighSafe" access="public" returntype="string" output="no" hint="This scans through a string, finds any characters that have a higher ASCII numeric value greater than 127 and automatically convert them to a hexadecimal numeric character">
<cfargument name="text" type="string" required="yes">
<cfscript>
var i = 0;
var tmp = '';
while(ReFind('[^\x00-\x7F]',text,i,false))
{
i = ReFind('[^\x00-\x7F]',text,i,false); // discover high chr and save it's numeric string position.
tmp = '&##x#FormatBaseN(Asc(Mid(text,i,1)),16)#;'; // obtain the high chr and convert it to a hex numeric chr.
text = Insert(tmp,text,i); // insert the new hex numeric chr into the string.
text = RemoveChars(text,i,1); // delete the redundant high chr from string.
i = i+Len(tmp); // adjust the loop scan for the new chr placement, then continue the loop.
}
return text;
</cfscript>
</cffunction>
</cfcomponent>
Hello, I would love to see your script as I work with CF5 but it’s not available anymore..
Comment by Etienne — January 23, 2009 @ 6:09 am |
The post has been updated with the code.
Comment by Ben — January 23, 2009 @ 8:22 am |