Solved

unescape in vb6

Posted on 2004-04-29
12
2,121 Views
Last Modified: 2012-05-04
Hi

How do I unescape a html formatted string (escaped in vbscript ) in vb.

or can some one pls suggest a better way of doing this:

I've a html formatted string that I've to store in a database and then open it in vb and print to text file for further processing.

Since storing html in database with all the quotes & spl characters and then reading it in vb gives error, I'm trying to escape the string before storing and then in VB I'm trying to unescape before printing.

Thanks
ak

0
Comment
Question by:ak3103
  • 6
  • 6
12 Comments
 

Author Comment

by:ak3103
Comment Utility
Also rt now I've a simple VB function  that converts hex to string, but does not work for many characters defined in html

Function unescape(ByVal s As String)
If s = "" Then
    unescape = ""
    Exit Function
End If
If Mid(s, 1, 1) <> "%" Then
    unescape = s
    Exit Function
End If
b = Split(s, "%")
For Each ele In b
    If ele <> "" Then
        a = Mid(ele, 1, 2)
        out = out & Chr(CInt("&H" & a)) & Mid(ele, 3)
    End If
Next
unescape = out
End Function

0
 
LVL 4

Expert Comment

by:Javin007
Comment Utility
"Since storing html in database with all the quotes & spl characters and then reading it in vb gives error, I'm trying to escape the string before storing and then in VB I'm trying to unescape before printing."

What kinds of errors are you getting and when?  Sounds like the best option would be to just fix those errors.

-Javin
0
 

Author Comment

by:ak3103
Comment Utility
if the html has dbl quotes then reading and using the string in VB gives errors
with single quotes stroing in the database itself is givng errors - this is easy to solve.

But HTML has got somany wierd characters, So I exactly dont know when I'll get an error - So I thought the best option is to escape and unescape - that way I can be sure that whatever is entered will be stored and retrieved with no probs

thanks
ak
0
 
LVL 4

Accepted Solution

by:
Javin007 earned 200 total points
Comment Utility
I'm still not sure what you mean by "escape and unescape"

But give me an example of EXACTLY what you're trying to do?  Sounds like it's as simple as dumping a single string of data to a database, then exporting it to a text file.  This should be VERY easy, regardless of what characters are used.

-Javin
0
 

Author Comment

by:ak3103
Comment Utility
Hey Javin

are you familiar with vbscript/javascript escape/unescape functions? these are built into the languages. -- not that u dont know, but the way u asked sounded like that.

escape function takes as input a html string and converts all the spl characters (except letters and digits) to 2 digit & 4digit hex form so that the html string would not contain any special chars like quotes etc.  

and unescape takes the encoded string and generates the html from it.

this is done when passing a html string between various functions/pages etc.  Now the string I obtain is from a rich text editor. so it has html in it.

the text file I'm writing also contains some other html. so if i dont escape and unescape, and if the html string I'm trying to write to the file has some extra html tags in it, ithose tags interfer with the tags in the main html file and the resulting html file is screwed up. to avoid this and also to avoid the mess with dbl quotes & single quotes when handling the strings I'm trying to do the escape/unescape.

ak

0
 
LVL 4

Expert Comment

by:Javin007
Comment Utility
Hrm.  Still not following.  I need an example of what you have, and what you want to do with it.  An actual example.  And I don't know much about HTML scripting, so I'm not familiar with the escape/unescape function.  But I do know VB very well.

-Javin
0
What Security Threats Are You Missing?

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.

 

Author Comment

by:ak3103
Comment Utility
here's an example

<hTML><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><font style=" font-size:'12px';color:'#000000';"  face='Arial'></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING="2">

is converted into the foll  string when escaped  , the %3c etc are hex code for < symbol and so on

%3ChTML%3E%3CTEXTFORMAT%20LEADING%3D%222%22%3E%3CP%20ALIGN%3D
%22LEFT%22%3E%3Cfont%20style%3D%22%20font-size%3A%2712px%27%3Bcolor
%3A%27%23000000%27%3B%22%20%20face%3D%27Arial%27%20%3E%3C/FONT
%3E%3C/P%3E%3C/TEXTFORMAT%3E%3CTEXTFORMAT%20LEADING%3D%222%22
%3E%3

as you can see the string contains <html> tag at the beginning. so if i write this into another file containing html already, the file is screwed up.

and also the initial html string there are dbl quotes and single quotes. when I read the string like this or try to write to database, it'll give an error.

so I'm converting the original string into the 2nd string (escape) and then writing into the database.

but then I've to convert the 2nd string into the first string (unescape) before wirting into the text file.

to escape/unescape there are built in functions in asp, but not in VB. so I'm tryiing to find if there is a function. the first function I posted works fine if there are 2digit hex codes. but if there is a 4digit hex code, it is giving an error.

ak



0
 
LVL 4

Expert Comment

by:Javin007
Comment Utility
Okay, I now see what the escape/unescape thing does, but what kind of database is this that it's giving you an error when you try to put the data into it?  You should have no problem parsing out the <HTML> tag with a simple string=mid$(string, 7)
Then dumping this:
<TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><font style=" font-size:'12px';color:'#000000';"  face='Arial'></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING="2">

Into a databse shouldn't give you any trouble at all.  You could then retrieve the data from the database, and dump that to a text file.  I'm still not catching why you're using the escape/unescaping?  Am I missing something, or do you need to know how to dump the text into the database?

-Javin
0
 

Author Comment

by:ak3103
Comment Utility
hey Javin ,

" or do you need to know how to dump the text into the database?"  :)

its not the database that will give an error when putting <html>,  but the actual file into which I'm trying to write the string.
also the <html> tag is just an example. any tag  will give an error in the output file if it not correctly placed.
so the usual process to avoid this is by escaping the html string and then unescaping the string wherever it has to be displayed.

I found some unescape functions but all of them were giving errors, So just trying to find if anyone knew a better function. If this doesnt work out, then I'll try manipulating the strings (escaping single & dbl quotes etc)

ak
0
 
LVL 4

Expert Comment

by:Javin007
Comment Utility
AHhhhhhhhhhhhhhhhhhh... I think I finally get it...
When you output this to the other file, (an HTML file) you want the HTML file to DISPLAY THE TEXT of the HTML?

-Javin
0
 

Author Comment

by:ak3103
Comment Utility
Hey Javin

yeah, sort of like that
anyways, I'll take care of it later. not so urgent rt now. thx for the help

ak
0
 
LVL 4

Expert Comment

by:Javin007
Comment Utility
Sorry I couldn't do more.  :/  Ask a moderator to remove the question so you get your points back.

-Javin
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

16 Experts available now in Live!

Get 1:1 Help Now