rss cfml

Remove or Clean High / Extended ASCII Characters in ColdFusion for XML Safeness

Reading time of 258 words
1 minute
Reading time of 258 words ~ 1 minute


Did you find this article helpful?
Please consider tipping me a coffee as a thank you.
Ko-fi Buy Me a Coffee
Did you find this article helpful? Please consider tipping me a coffee or three as a thank you.
Tip using Ko-fi or Buy Me a Coffee

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.

The example code can be found below or download from GitHub.

<!----- xml-high-safe.cfc 1.0 by Ben Garrett : 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>

Written by Ben Garrett

Did you find this article helpful?
Please consider tipping me a coffee as a thank you.
Ko-fi Buy Me a Coffee
Did you find this article helpful? Please consider tipping me a coffee or three as a thank you.
Tip using Ko-fi or Buy Me a Coffee