Welcome to WebmasterWorld Guest from 18.205.176.85

Forum Moderators: ocean10000

Message Too Old, No Replies

Escaping single and double quote from string

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

Junior Member

10+ Year Member

joined:Feb 13, 2004
posts: 64
votes: 0


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, "'", "\'"), """, "\"")%>

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

New User

10+ Year Member

joined:Oct 14, 2008
posts:4
votes: 0


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]