XmlSafeText(txt)
Last updated July 12, 2009
Version: 0 | Requires: CF8 | Library: StrLib
Description:
This was inspired by the xmlFormat2 function. I was worried about the performance of that function, however, and so rewrote the same basic functionality using the REMatch function that was introduced in CF8.
Return Values:
Returns a string.
Example:
<cfset s = "text with high-ascii (#chr(982)#) char.">
<cfoutput>
<pre>
#xmlFormat(s)#
#xmlSafeText(s)#
</pre>
</cfoutput>
Parameters:
Name | Description | Required |
---|---|---|
txt | String to format. | Yes |
Full UDF Source:
<!---
Replacement for XmlFormat that also replaces all special characters.
@param txt String to format. (Required)
@return Returns a string.
@author David Hammond (dave@modernsignal.com)
@version 0, July 12, 2009
--->
<cffunction name="XmlSafeText" hint="Replaces all characters that would break an xml file." returnType="string" output="false">
<cfargument name="txt" type="string" required="true">
<cfset var chars = "">
<cfset var replaced = "">
<cfset var i = "">
<!--- Use XmlFormat function first --->
<cfset txt = XmlFormat(txt)>
<!--- Get all other characters to replace. --->
<cfset chars = REMatch("[^[:ascii:]]",txt)>
<!--- Loop through characters and do replace. Maintain a list of characters already replaced to avoid duplicate work. --->
<cfloop index="i" from="1" to="#ArrayLen(chars)#">
<cfif listFind(replaced,chars[i]) is 0>
<cfset txt = Replace(txt,chars[i],"&##" & asc(chars[i]) & ";","all")>
<cfset replaced = ListAppend(replaced,chars[i])>
</cfif>
</cfloop>
<cfreturn txt>
</cffunction>
Search CFLib.org
Latest Additions
Raymond Camden added
QueryDeleteRows
November 04, 2017
Leigh added
nullPad
May 11, 2016
Raymond Camden added
stripHTML
May 10, 2016
Kevin Cotton added
date2ExcelDate
May 05, 2016
Raymond Camden added
CapFirst
April 25, 2016