Solved

Replacing Apostrophes in Classic ASP

Posted on 2010-11-17
29
1,959 Views
Last Modified: 2012-05-10
I have a function that replaces apostrophes with ~ or ~~ for my datbase insertion; however, whenever something is pasted into the textbox from office, the apostrophe takes on a different character.

' is what I have programmed for; however, ’ is what is created via office (straight vs curly).

I tried the following code but it does not recognize the curly apostrophes:

Function ReplaceQuotesSave(strData)
      strData = Replace(strData, "'", "~")
      strData = Replace(strData, "’", "~")
      strData = Replace(strData, """", "~~")
      strData = Replace(strData, "“", "~~")      

      ReplaceQuotesSave = Trim(strData)
End Function

It works great on the straight apostrophe (').

Anyone know how to deal with the curly apostrophes in asp?
0
Comment
Question by:dzirkelb
  • 13
  • 10
  • 3
  • +2
29 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
>I have a function that replaces apostrophes with ~ or ~~ for my datbase insertion
you don't need that if you do the insert properly
example with stored procedure: http://www.freevbcode.com/ShowCode.Asp?ID=3687
example with plain insert: http://social.msdn.microsoft.com/Forums/en-US/sqldataaccess/thread/d1e9ec4c-7fbd-4b04-ba37-4758f0300fec

once you use the parameters instead of building the insert sql ad-hoc, you don't need to replace any character at all for the sql ...
0
 

Author Comment

by:dzirkelb
Comment Utility
I am not interested in that route at this time, I would just like my specific issue resolved.
0
 
LVL 7

Expert Comment

by:niralshah
Comment Utility
try this

strData = Replace(strData, CHR(39), "~")
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
well...
Function ReplaceQuotesSave(strData)

      strData = Replace(strData, "'", "~")

      strData = Replace(strData, "’", "~")

      strData = Replace(strData, "`", "~")

      strData = Replace(strData, "‘", "~")

      strData = Replace(strData, "’", "~")

      strData = Replace(strData, """", "~~")

      strData = Replace(strData, "“", "~~")      

      strData = Replace(strData, "”", "~~")      



      ReplaceQuotesSave = Trim(strData)

End Function

Open in new window

0
 

Author Comment

by:dzirkelb
Comment Utility
Ok, I did a little more troubleshooting, and here is what I found.

I am just printing to the screen a request.form of the data, and that is dropping the curly apostrophe, without any code involved.

As an example:

test’s was placed into a textbox and posted to another page.  The other page prints this to the screen:

tests

The first line of code on the page is response.write(requst.form("txtData"))

So, any ideas on what would cause this?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
for the output, see the HTMLencode method:
http://www.w3schools.com/ASP/met_htmlencode.asp
0
 

Author Comment

by:dzirkelb
Comment Utility
response.write(Server.HTMLEncode("There’s")) outputs There’s

response.write(Server.HTMLEncode(request.form("txtData"))) outputs Theres

I am definately confused.
0
 
LVL 1

Expert Comment

by:imnict
Comment Utility
to add, you will want to look for the other ASCII variants that may be causing you grief.

strData = Replace(strData, CHR(145), "~")
strData = Replace(strData, CHR(146), "~")
0
 

Author Comment

by:dzirkelb
Comment Utility
I think this is happening before I can even do a replace function, as displaying a request.form to the page strips the curly apostrophe, so the replace function won't even find the curly apostrophe in the first place.
0
 

Author Comment

by:dzirkelb
Comment Utility
response.write(Server.HTMLEncode("There**curlyapostrophe**s")) outputs There**curlyapostrophe**s
response.write("There**curlyapostrophe**s") outputs There’s
response.write(Server.HTMLEncode(request.form("txtData"))) outputs Theres
response.write(request.form("txtData")) outputs Theres

where **curlyapostrophe** is equal to a curly apostrophe

0
 
LVL 1

Expert Comment

by:imnict
Comment Utility
Try these:

strData = Replace(Server.HTMLEncode(strData), "%27", "~")
strData = Replace(Server.HTMLEncode(strData), "%60", "~")
strData = Replace(Server.HTMLEncode(strData), "%92", "~")
strData = Replace(Server.HTMLEncode(strData), "%93", "~")
strData = Replace(Server.HTMLEncode(strData), "%B4", "~")

Unicode character reference: Unicode Character Reference
0
 

Author Comment

by:dzirkelb
Comment Utility
It still just strips the curly apostrophe and does not display anything.

Could the reason behind this be due to some font not being installed on the server and it is not recognizing it at all?
0
 
LVL 1

Expert Comment

by:imnict
Comment Utility
It's not likely to be a font on the server.  More likely is to determine what character is really in the string.

Quote:
response.write("There**curlyapostrophe**s") outputs There’s

If you view the source html rendered, what is actually being sent to the browser?
0
 

Author Comment

by:dzirkelb
Comment Utility
There&#8217;s<br><Br>There’s<br><br>Theres

Is outputed by going to view source

response.write(Server.HTMLEncode("There’s")&"<br><Br>")
response.write("There’s<br><br>")
response.write(Server.HTMLEncode(request.form("txtData")))

is the code producing that text
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Accepted Solution

by:
imnict earned 500 total points
Comment Utility
You have something more going on here.  Testing your example here gives renders quite different:

I used this:
response.write(Server.HTMLEncode("There’s")&"<br><Br>")
response.write("There’s<br><br>")

To get this in the browser:
There’s

There’s

Which is rendered in html as:
There&#8217;s<br><Br>There’s<br><br>

What character set is being used on the server and client browser?

Also, when I changed to using URLEncode, I got results that may work for you:
response.write(Server.urlEncode("There’s")&"<br><Br>")

produces:
There%92s

So -

strData = Replace(Server.URLEncode(strData), "%27", "~")
strData = Replace(Server.URLEncode(strData), "%60", "~")
strData = Replace(Server.URLEncode(strData), "%92", "~")
strData = Replace(Server.URLEncode(strData), "%93", "~")
strData = Replace(Server.URLEncode(strData), "%B4", "~")

You will want these checks to be after the remaining character checks that you can successfully catch without encoding the form input.
0
 

Author Comment

by:dzirkelb
Comment Utility
How do I find the character set being used on the server and client browser?  I think that is the problem.
0
 
LVL 1

Expert Comment

by:imnict
Comment Utility
Which platform are you running?  IISx, or some mod_whatever on apache?
0
 

Author Comment

by:dzirkelb
Comment Utility
iis6.0 on a server 2003 box
0
 
LVL 1

Expert Comment

by:imnict
Comment Utility
Check in the control panel, Regional and Language Options.

Check in Internet Explorer on the server under tools | options, General Tab, Languages to see what is installed there as well.

Also check the top of your asp page.  Frontpage, Web Developer Express, Visual Studio, Dreamweaver...  like to add a language line at the top of the page.  It would look something like:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>

0
 

Author Comment

by:dzirkelb
Comment Utility
IE language is English (United States) [en-us]

regional langauge keyboard settings are set to US, and the Turn Off advanced text services is checked.

For adding the code, I get this error:

Active Server Pages error 'ASP 0245'

Mixed usage of Code Page values

/EzineTech-edit-individual-save.asp, line 1

The @CODEPAGE value specified differs from that of the including file's CODEPAGE or the file's saved format.
0
 
LVL 1

Expert Comment

by:imnict
Comment Utility
Ok - so you have an included file that has this line already defined or it already existed in your file elsewhere down the line list.  Once you find it, from the error it differs from the default.  What is it set to?
0
 

Author Comment

by:dzirkelb
Comment Utility
This is all I have on the coded page:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
response.write("hello world")
%>

and I still get the error.
0
 
LVL 1

Expert Comment

by:imnict
Comment Utility
Do you have any additional headers defined for the iis site?
0
 

Author Comment

by:dzirkelb
Comment Utility
I do have a web.config file for some .net pages.

And, I am seeing in the custom http headers section that there is one for X-Powerd-By: ASP.Net
0
 
LVL 1

Expert Comment

by:imnict
Comment Utility
Remove the codepage line from your test asp page and insert this:

<%
Response.Write(Session.CodePage)
%>

What does it return?
0
 
LVL 1

Assisted Solution

by:imnict
imnict earned 500 total points
Comment Utility
Also, in your web.config for asp.net, what is in the following keys?

<globalization
            requestEncoding="utf-8"
            responseEncoding="utf-8"  />
0
 

Author Comment

by:dzirkelb
Comment Utility
<%
Response.Write(Session.CodePage)
%>

returns 1252

for the web.config file, there is no flobalization tag, it was not created in visual studio, so it is a stripped down config file.
0
 
LVL 29

Expert Comment

by:Kumaraswamy R
Comment Utility
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now