Freakish ASP quote handling

This is a discussion on "Freakish ASP quote handling" within the Classic ASP section. This forum, and the thread "Freakish ASP quote handling are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > Classic ASP

Notices




Closed Thread
 
LinkBack Thread Tools
  #1  
Old Sep 17th, 2003, 13:50
Reputable Member
Join Date: Sep 2003
Location: USA
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
Freakish ASP quote handling

I've got this part of my site where people enter information into a textbox.

I learned long ago (and far away ) that with ASP you need to replace ' with '' and " with "" or it choaks because it thinks it's a string terminater.

I've got a function that does this (and other things) for me...

Code: Select all
function switchQuotes(text)
	while instr(text, "  ") > 0
		text = Replace(text, "  ", " ")
	wend
	text =  Replace( _
		Replace( _
		Replace( _
		Replace( _
		Replace( _
		Replace( Trim(text), "'", "''"), _
				    "\n", " "), _
				    vbcrlf, " "), _
				    "|", ""), _
				    "%", ""), _
				    chr(34), """")
'	response.write "switchme!"
	switchQuotes = text		
end function
My problem is that *sometimes* this backfires. It only seems to happen during the editing of the text. But this review works fine:
This book is the follow-up to Surely You're Joking, Mr. Feynman: Adventures of a Curious Character and I recommend that it should be read only after reading that volume. The book is organized as a series of loosely related chapters that illustrate Feynman's unique perspective and his ways of interacting with the world. It is less personal than the preceding book in that some of the chapters are more about science-related topics and less about Feynman. Some of the chapters fill in details that were introduced in "Surely You're Joking"

but this one doesn't:
The comments of "A Reader" below obviously mean that the book is truthful and spot on. Otherwise, why would he get so upset! I enjoyed this book and find it scary that so many people get their news from these liars. It's not possible for everyone in America to confirm facts that the media spit out. America should demand truth in journalism. Whether you are right or left, at least you want to know the truth, dont you? Thank you Al Franken.

If I edit the second review by adding "Testing 1,2" to the end, that part is not added and It's looks like It''s.

When I display the SQL to the page, it's correct:
UPDATE TblReviews SET Title='Lies and the Lying Liars Who Tell Them: A Fair and Balanced Look at the Right', Author='Al Franken', Illustrator='', LevelID=5, Genre='Biography', Culture=134, Review='The comments of "A Reader" below obviously mean that the book is truthful and spot on. Otherwise, why would he get so upset! I enjoyed this book and find it scary that so many people get their news from these liars. It''s not possible for everyone in America to confirm facts that the media spit out. America should demand truth in journalism. Whether you are right or left, at least you want to know the truth, dont you? Thank you Al Franken. Testing 1,2' WHERE ISBN='0-525-94764-7' AND Reviewer=1852

But what I get on my display page is wrong:
The comments of "A Reader" below obviously mean that the book is truthful and spot on. Otherwise, why would he get so upset! I enjoyed this book and find it scary that so many people get their news from these liars. It''s not possible for everyone in America to confirm facts that the media spit out. America should demand truth in journalism. Whether you are right or left, at least you want to know the truth, dont you? Thank you Al Franken.

AAAAAAAAAAAAAAAAAAAAAAAARG!

This seems to be a bug sort of thing, but I have no idea how to work around this..

Does anyone know?

Thanks
jakyra
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!

  #2  
Old Sep 17th, 2003, 14:06
Rob's Avatar
Rob Rob is offline
Webforumz Founder
Join Date: Jul 2003
Location: Southern UK
Age: 34
Posts: 3,186
Blog Entries: 7
Thanks: 27
Thanked 23 Times in 20 Posts
SQL is fine with double quotes...

You only need to double up apostrophe's
__________________
Click the 'Thanks!' button if this post has helped you

Rob - Webforumz Founder
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #3  
Old Sep 17th, 2003, 14:11
Junior Member
Join Date: Aug 2003
Location: United Kingdom
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
I think there are two issues that you are getting mixed up with here between single quotes and double quotes.

If you are building up a query to a database, then the only replace you do is single quotes. It deals with double quotes just fine.

However if you are building up a string in ASP, then you only need to replace double quotes.

So for example.

MyString = "Hey ""Nick"", you're here!"

MyString now contains the text;

Hey "Nick", you're here!

Note that you don't have to double up the single quotes.

But if you then want to put this in the database, you need to double up the single quotes.

Database.Execute "INSERT INTO dasf (asdf) values (" & Replace(MyString, "'", "''") & ")"

But you don't have to do a replace on the double quotes.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #4  
Old Sep 17th, 2003, 14:11
Rob's Avatar
Rob Rob is offline
Webforumz Founder
Join Date: Jul 2003
Location: Southern UK
Age: 34
Posts: 3,186
Blog Entries: 7
Thanks: 27
Thanked 23 Times in 20 Posts
You could simplify that function a lot. All it has to do is take a string and make it SQL safe!!

Try this:-
Code: Select all
FUNCTION sqlsafe(s) 
pos = InStr(s, "'")
      While pos > 0
           s = Mid(s, 1, pos) & "'" & Mid(s, pos + 1)
           pos = InStr(pos + 2, s, "'")
      Wend
sqlsafe=s
END FUNCTION
or better still, this:-:-
Code: Select all
FUNCTION sqlsafe(s) 
   sqlsafe=replace(s,"'","''")
END FUNCTION
__________________
Click the 'Thanks!' button if this post has helped you

Rob - Webforumz Founder
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #5  
Old Sep 17th, 2003, 14:14
Junior Member
Join Date: Aug 2003
Location: United Kingdom
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
p.s. just noticed something, you've got the following;

Replace(...., chr(34), """")

As a replace. note that """" is infact only one double-quote. Remember in ASP, you have to put two "" to signify the character, not a string delimiter. So 4 x " means one to open up the string, two to signify a double quote, then one to close the quote. So what you need is;

Replace(...., """", """""")

Or to make things simpler;

Replace(...., chr(34), chr(34) & chr(34))


Note that I don't know if this is at all related to your problem, but I thought I'd point it out.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #6  
Old Sep 17th, 2003, 14:15
Junior Member
Join Date: Aug 2003
Location: United Kingdom
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
<blockquote id="quote"><font size="1" face="geneva, verdana, arial" id="quote">quote:<hr height="1" noshade id="quote">Originally posted by Rob

You could simplify that function a lot. All it has to do is take a string and make it SQL safe!!

Try this:-
Code: Select all
FUNCTION sqlsafe(s) 
pos = InStr(s, "'")
      While pos > 0
           s = Mid(s, 1, pos) & "'" & Mid(s, pos + 1)
           pos = InStr(pos + 2, s, "'")
      Wend
sqlsafe=s
END FUNCTION
<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">

Or infact just MyString = Replace(MyString, "'", "''")
I think you're look at old code

However his function does more than that - it removes double spacing, trims it, removes various invalid characters etc.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #7  
Old Sep 17th, 2003, 14:29
Rob's Avatar
Rob Rob is offline
Webforumz Founder
Join Date: Jul 2003
Location: Southern UK
Age: 34
Posts: 3,186
Blog Entries: 7
Thanks: 27
Thanked 23 Times in 20 Posts
see the addition to the above post Nick... I edited the topic while you were posting.

__________________
Click the 'Thanks!' button if this post has helped you

Rob - Webforumz Founder
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #8  
Old Sep 17th, 2003, 17:11
Reputable Member
Join Date: Sep 2003
Location: USA
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
I got it to work!

Thanks much for everyone's comments. Although I'm leaving my function complicated

I did take out the " --> "" since it's unecessary.

Thanks everyone!

jakyra
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Closed Thread

Tags
freakish, asp, quote, handling

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
looking for job quote calculator bonnit PHP Forum 3 May 17th, 2008 13:29
Quote extender plato JavaScript Forum 10 Aug 29th, 2007 13:16
Website Quote Deano Website Planning 0 Jan 11th, 2007 16:31
stock quote applet... wlandymore Other Programming Languages 0 Nov 26th, 2006 16:41


All times are GMT. The time now is 04:06.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42