Create string with values containing double quotes

H

Hels Bells

Hi,

I'm looking to do some manipulation on a string containing html code
in asp which will involve me either using some regular expressions or
just plain old simple replace functionality. The text that I want to
use will have double quotes in it (as it's xhtml code) so I can't set
it as a string value and is actually brought into the page from a
content managed element (the details of which I don't think I'll need
to go into but needless to say it produces some valid xhtml)

What I want to be able to do is

<%
sTest = Replace("<a href="#">test link</a>", "<", "&lt;")
Response.Write (sTest)
%>

This will obviously give a syntax error because of the double quotes
surrounding the href attribute. The first parameter isn't built up
from other strings in the code before but is pulled directly from a
fixed content management system element which I can't format any other
way. So I can't do

<%
s1 = "<a href="#">test link</a>"
sTest = Replace(s1, "<", "&lt;")
Response.Write sTest
%>

I need the result of this replace to then be put into an xml attribute
so I need to convert the < > and " characters to &lt; &gt; and &quot;
respectively.

Anyone have any ideas of how I could get this to work?

Many thanks
 
B

Bob Barrows [MVP]

Hels said:
Hi,
This will obviously give a syntax error because of the double quotes
surrounding the href attribute.

In vbscript, special characters are "escaped" by doubling them

<%
sTest = Replace("<a href=""#"">test link</a>", "<", "&lt;")
Response.Write (sTest)
%>
 
E

Evertjan.

Hels Bells wrote on 07 aug 2007 in microsoft.public.inetserver.asp.general:
I'm looking to do some manipulation on a string containing html code
in asp
[..]

<%
s1 = "<a href="#">test link</a>"

1 use single quotes in html:

s1 = "<a href='#'>test link</a>"

or
2 double the quotes in a vbscript string:

s1 = "<a href=""#"">test link</a>"
 
B

Bob Barrows [MVP]

Hels said:
Hi,

I'm looking to do some manipulation on a string containing html code
in asp which will involve me either using some regular expressions or
just plain old simple replace functionality. The text that I want to
use will have double quotes in it (as it's xhtml code) so I can't set
it as a string value and is actually brought into the page from a
content managed element (the details of which I don't think I'll need
to go into but needless to say it produces some valid xhtml)

What I want to be able to do is

<%
sTest = Replace("<a href="#">test link</a>", "<", "&lt;")
Response.Write (sTest)
%>

Oh, and use htmlencode instead of all those replace statements:

sTest = Server.htmlencode("<a href=""#"">test link</a>")

I won't comment on the use of "#" for the href
 
D

delimiter

Oh, and use htmlencode instead of all those replace statements:

sTest = Server.htmlencode("<a href=""#"">test link</a>")

I won't comment on the use of "#" for the href

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

I can't double up the quotes (which I normally would) as the html
string is generated by a third party application that I can't alter
until it's in my asp page which is the main part of the problem. I
could use javascript to alter it but the manipulated string needs to
be available for all users in the html.
 
B

Bob Milutinovic

delimiter said:
I can't double up the quotes (which I normally would) as the html
string is generated by a third party application that I can't alter
until it's in my asp page which is the main part of the problem. I
could use javascript to alter it but the manipulated string needs to
be available for all users in the html.

So, the "third party application" passes you a string. Let's call that
string sString (for lack of a better name).

sString can contain absolutely any character - quotes, nulls, backspaces
(chr(8)), deletes (chr(127)), you name it.

VBScript doesn't care what that string contains - to it, it's just a string.

Now, you come along with...

sTest = Replace(sString, "<", "&lt;")

.... And the task will be performed - no matter how many quotes or other
"unusual" characters there are in the string.

As Bob Barrows explained though, if all you're doing is converting HTML
delimeters to literals, you're best off to use Server.HTMLencode() - it's
much faster and infinitely easier.

--
Bob Milutinovic
Cognicom - "Australia's Web Presence Specialists"
http://www.cognicom.net.au/
telephone (0417) 45-77-66
facsimile (02) 9824-2240
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top