Solved

Replacing Apostrophes in Classic ASP

Posted on 2010-11-17
29
2,050 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]
ID: 34154749
>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
ID: 34154800
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
ID: 34154807
try this

strData = Replace(strData, CHR(39), "~")
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34154900
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
ID: 34154946
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]
ID: 34155008
for the output, see the HTMLencode method:
http://www.w3schools.com/ASP/met_htmlencode.asp
0
 

Author Comment

by:dzirkelb
ID: 34155062
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
ID: 34155091
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
ID: 34156098
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
ID: 34156188
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
ID: 34156669
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
ID: 34156743
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
ID: 34156781
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
ID: 34156872
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
 
LVL 1

Accepted Solution

by:
imnict earned 500 total points
ID: 34157154
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
ID: 34157195
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
ID: 34158054
Which platform are you running?  IISx, or some mod_whatever on apache?
0
 

Author Comment

by:dzirkelb
ID: 34158312
iis6.0 on a server 2003 box
0
 
LVL 1

Expert Comment

by:imnict
ID: 34158429
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
ID: 34158516
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
ID: 34158539
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
ID: 34158693
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
ID: 34159259
Do you have any additional headers defined for the iis site?
0
 

Author Comment

by:dzirkelb
ID: 34159301
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
ID: 34159587
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
ID: 34159615
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
ID: 34159661
<%
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
ID: 34387724
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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