I've been working on improving the RSS feeds generated by Lighthouse. One persistent problem is that some RSS readers (IE, for instance) will choke on a lot of special characters, such as those pasted from Microsoft Word. I have previously put in place code to replace many of those characters, but came across another one today that wasn't replaced. I knew I needed a better solution.
On CFlib.org, I found a function called xmlFormat2 that smartly avoids maintaining a list of characters to replace, and just replaces all characters not in a list of "good" characters. That makes sense. And it works.
I was concerned, though, about the performance of the function, and I thought it could be done better. Using the REMatch function (introduced in ColdFusion 8), I was able to make the function both simpler and much faster. My tests so far have been limited, but it has so far handled everything I have thrown at it. And here it is:
<cffunction name="XmlSafeText" hint="Replaces all characters that would break an xml file." returnType="string" output="false">
<cfargument name="txt" hint="String to format" type="string" required="true">
<cfset var chars = "">
<cfset var replaced = "">
<!--- 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="char" array="#chars#">
<cfif ListFind(replaced,char) is 0>
<cfset txt = Replace(txt,char,"&##" & asc(char) & ";","all")>
<cfset replaced = ListAppend(replaced,char)>
It should be possible to use it as a replacement for the built in XmlFormat function. Let me know if you run into any problems with it.
Posted on July 6, 2009 7:08:07 PM EDT by David Hammond
We wouldn’t have gotten where we are today without your support over the years. Modern Signal has always been a great partner to us.
Modern Signal worked with us to understand our needs and figure out what solution would work best for us. Our Lighthouse CMS is perfectly suited to our website goals. When we later needed to modify the CMS, they again took the time to understand exactly what was needed and then built that functionality rather than delivering a cookie cutter solution.
Modern Signal has a professional staff that was very responsive to our needs during all phases - scoping, developing, implementing and maintaining - of our project. We have been pleased with their ability to deliver quality work on time and on budget. If given the opportunity, I would work with them again.
I felt as if my company was their only client. They responded to my needs quickly and efficiently despite short turn around time and intense demands.
Modern Signal understands our business - from future needs to current limitations - so their solutions are always scalable, solid, and service-oriented.
Modern Signal has been a great partner for us for over the past 10 years. As our business grew and our needs changed, Modern Signal was able to work with us to adjust our website platform in the ever-changing online world. Their service and response level has been second to none, and we've been never been happier with our relationship with them.
This was by far the smoothest website redevelopment I have ever experienced. Modern Signal was a wonderful company to work with and we greatly value our working relationship.
I love working with Modern Signal! Their CMS is very easy to use and they are incredibly responsive to questions or challenges I bring them.
Modern Signal significantly enhanced our site to be more efficient and user-friendly. They provide excellent customer service with timely and cost-effective solutions.