Link to home
Start Free TrialLog in
Avatar of ak3103
ak3103

asked on

unescape in vb6

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

Avatar of ak3103
ak3103

ASKER

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

"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
Avatar of ak3103

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Javin007
Javin007
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ak3103

ASKER

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

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
Avatar of ak3103

ASKER

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



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
Avatar of ak3103

ASKER

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
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
Avatar of ak3103

ASKER

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
Sorry I couldn't do more.  :/  Ask a moderator to remove the question so you get your points back.

-Javin