Forum Moderators: open

Message Too Old, No Replies

Escaping single and double quote from string

         

jomoweb

7:55 pm on Oct 22, 2008 (gmt 0)

10+ Year Member



I am fairly new to ASP and have noticed that apostrophes in data driven variables can cause code to break or not display since a single quote is a comment.

I have an instance where I am calling a string from a database text field to show content in a mouseover tooltip. It works great as long as there is not a single or double quote in the text.

What is the best way to escape single and double apostrophes?

Code:
<%=strDescription%>

I tried this, but it seems pretty silly and did not work ...

<%=Replace(Replace(strDescription, "'", "\'"), """, "\"")%>

Matt_C

8:48 am on Oct 23, 2008 (gmt 0)

10+ Year Member



There are a few ways to overcome this, probably the most simple is the following

<%
strDescription=replace(strDescription,"'","&#39;")
strDescription=replace(strDescription,"""","&quot;")
%>

Try that

hth

Matt

[edited by: Matt_C at 8:55 am (utc) on Oct. 23, 2008]