Replacing Double Quotes with REReplace()

May 16, 2007

Faced two problems whilst looping over some database entries today (with data coming from many RSS feeds, so essentially data which could contain anything).. which I can imagine other people stumbling into on occasion.

Problem One: XHMTL Which doesn't validate due &'s in a non web format

Problem Two: Title attributes in href tags closing too early due to double quotes in the string

i.e:

 

<a href="#Link#" title="Go to: #title#">#title#</a>

 

If

Link = http://www.somedomain.com/foo.cfm?id=1&somevar=2

and title = John Doe on "Why Should I"

Then you'd end up with the following:

 

<a href="http://www.somedomain.com/foo.cfm?id=1&somevar=2" title="John Doe on "Why Should I"">John Doe on "Why Should I"</a>

This has two issues: the

&somevar

should be written

&amp;somevar

, and the browser sees the title attribute as

"John Doe on"

, and then has no idea with the rest, which it assumes is your attempt as an invalid attribute in XHTML.

 

How to fix: Use HTMLEDITFORMAT() to escape the &'s automatically, and RegularExpression Replace - REREPLACE() - to replace the double quotes in the string with single ones.

 

<a href="#htmlEditFormat(Link)#" title="Go to: #REReplace(title,"[""]","'","ALL")#">#Title#</a>

 

Comments

Write your comment



(it will not be displayed)