SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Location
    Albany, NY
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question ASP replace word special chars in string variables

    Monthly I receive a calendar of events for posting on 2 web sites.(word document)

    1. I open the word doc and paste the events into the first site (SharePoint site)

    2. I then export the sharepoint calendar into an access DB

    3. I then use classic ASP to take the events out of the export DB and
    add them to the live DB on the second site.

    Here’s where I run into problems:

    When I extract the events from the export DB into ASP string variables, I need to find word special chars such as smart quotes and replace them with regular quotes. This is because if I leave them, they display on the web as a small square (unrecognized character).

    I have tried the following code to replace the special chars:
    titleeNew = replace(titleeNew, "“", """")
    titleeNew = replace(titleeNew, "”", """")
    titleeNew = replace(titleeNew, "'", "'")
    titleeNew = replace(titleeNew, "’", "'")

    Unfortunately the special characters I’m looking to replace are not being found in my string variable. I even tried the inSTR function to see if the special characters exist in the string and get back position 0, which means that even though the special chars are in the export DB, when I query them from the DB, somehow they are not ending up in the string variable.

    What can I do?

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Can you try Paste-Special and use unformatted text into the SharePoint site?

    Another thing to try is to paste some text (with the special chars) into a test string in an ASP page, then loop through each character and return it's ASC value. Example:
    Code ASP:
    <%
    str = "This “is some text” with special chars"
    For i = 1 to Len(str)
    	char = Mid (str,i,1)
    	ascval = Asc(char)
    	Response.Write "Character '" & char & "' has ASC value " & ascval & "<br>"
    Next
    %>
    Once you know the ASC values you could use the Chr() command in your Replace:
    Code ASP:
    titleeNew = replace(titleeNew, Chr(147), """")
    titleeNew = replace(titleeNew, Chr(148), """")
    (147 and 148 are the opening and closing Word quote marks).
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Location
    Albany, NY
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's not exactly what I want to do. The reason I say that is that I actually want to replace characters like smart quotes and smart apostrophy with regular qoutes and regular apostrophy. I don't think I can find and replace in word. My thought was to import word to sharepoint and export to access. I would then find and replace in ASP as I import to other site.

    Mostly I'm trying to do this with as few steps as possible. That way if the boss has to post the calendar one month, it will be easy for her to do.

    Mike

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You may have misunderstood what I was saying (or missed my edit).

    Using the mechanism I show you'll be able to determine the special characters and their Asc codes, so will be able to replace the Chr coded characters with the normal characters you want, and have this already built in to your ASP script.
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Location
    Albany, NY
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok. i did miss your edit...

    I can lookup the char codes and use those. maybe my copying the special char from word directly into my ASP code does not work. thanks

  6. #6
    SitePoint Addict itHighway's Avatar
    Join Date
    Jan 2008
    Location
    GUJ
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I now use Regular Express to replace any values from a string as for some reason I found it working more efficiently then the 'Replace' function. Below is the code I use, you can change it according to your req.
    Code ASP:
    	Private function fChr(byVal sfield)
     
    		Set objRegExp = New RegExp
     
    			objRegExp.IgnoreCase = True
    			objRegExp.Global = True
    			objRegExp.Pattern = " & " 
    			sfield = objRegExp.replace(sfield, " &amp; ")
     
    			objRegExp.Pattern = Chr(34)
    			sfield = objRegExp.replace(sfield, """)
     
    			objRegExp.Pattern = Chr(13)
    			sfield = objRegExp.replace(sfield, "
")	
     
    			objRegExp.Pattern = "<br>"
    			sfield = objRegExp.replace(sfield, "
")				
     
    			for A = 158 to 255
    				objRegExp.Pattern = Chr(A)
    				sfield = objRegExp.replace(sfield, "&#"&A&";")	
    			next									
     
    		Set objRegExp = nothing
     
    		fChr = sfield
    	end function

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Location
    Albany, NY
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks ITHighway... you've saved the day and made my morning. now I just need to find the chr() value of all the characters I need to replace and all will be well.

  8. #8
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Which is where my original example post comes in. Just paste a copy of text that contains the characters into str and run the page - you'll get the Asc values (hence required Chr codes) for each character.
    Ian Anderson
    www.siteguru.co.uk

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Location
    Albany, NY
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    Function fixit(strText)
    strText = replace(strText, chr(147), """")
    strText = replace(strText, chr(148), """")
    'strText = replace(strText, """", "&quot;")
    strText = replace(strText, chr(145), "&#39;")
    strText = replace(strText, chr(146), "&#39;")
    strText = replace(strText, chr(39), "&#39;")

    fixit = strText
    end Function


    when I call the function, fixit(titlee), no replace occurs. I can't see why. now the smart quotes are not getting replaced with regular quotes.

    I've checked the ascii table and 147 & 148 are the correct values for the chr function.

    what am I missing? the weird thing is that the code was working fine on Friday.

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Location
    Albany, NY
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to confuse things further...

    I added a few more replace statements to try and get the replace to occur:

    strText = replace(strText, "“", """")
    strText = replace(strText, "”", """")
    strText = replace(strText, "’", "&#39;")
    strText = replace(strText, "‘", "&#39;")

    This seemed to get the conversion to occur. I did test it on a dummy page and was it work finally. Hen I put those extra lines of code in my original page, I get inconsistant results based upon when I view the file. it's as though sometimes the conversion occurs and sometimes it doesn't. can this be a bug with IIS in windows 7?

    Mike

  11. #11
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Location
    Albany, NY
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just noticed that I was not calling the replace function in my code. I don't know why but adding the function call solved things. Also a smack palm to forehead helped.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •