HtmlEncode in VB

Hi,

I need to generate HTML code
from a VB application.

In order for the HTML code to be parsed properly by he browser, I need to properly encoded. I know that ASP's Server object has an HTMLEncode method.

I did not manage to use the Server object from my VB application. Is there any other way of encoding HTML code.

Thanks,
Ralph
rakhrasAsked:
Who is Participating?
 
LorenGConnect With a Mentor Commented:
Rakhras,

It turns out I need the same thing.  There is no info anywhere on what is the best way to do this.  I wrote this Javascript below.  I have no idea if this is the safest way to do this, but it is working very well for displaying HTML and script source on a web page for me.  I added a quick conversion to VB at the bottom.

Javascript:
function clientHTMLEncode(sSrc)
{
var sRet = sSrc;
sRet = sRet.replace(/&/g,"&")
sRet = sRet.replace(/</g,"&lt;")
sRet = sRet.replace(/>/g,"&gt;")
sRet = sRet.replace(/ /g,"&nbsp;")
sRet = sRet.replace(/"/g,"&quot;")
return sRet;
}

I am not sure if you know javascript, so here is the VB Version (Unchecked)

Visual Basic:
function HTMLEncode(byval sSrc as String) as String

Dim sOut as String
sOut = sSrc
sOut = Replace(sOut,"&","&amp;")
sOut = Replace(sOut,"<","&lt;")
sOut = Replace(sOut,">","&rt;")
sOut = Replace(sOut," ","&nbsp;;")
sOut = Replace(sOut,"""","&quot;")

HTMLEncode = sOut

End Function


- Loren
0
 
LorenGCommented:
You will need WinINet DLL which comes with IE4 or greater
---------------------------------------

Here is the API function call:
---------------------------------------
Private Declare Function InternetCanonicalizeUrl Lib "wininet.dll" Alias "InternetCanonicalizeUrlA" (ByVal lpszUrl As String, ByVal lpszBuffer As String, lpdwBufferLength As Long, ByVal dwFlags As Long) As Long


Here is a wrapper function:
---------------------------------------
Public Function URLEncode(ByVal sURL As String) As String

Dim sBuff As String
sBuff = Space$(Len(sURL) * 2)

Dim sLen As Long
sLen = Len(sBuff) - 1

Call InternetCanonicalizeUrl(sURL, sBuff, sLen, 0)
URLEncode = Left(sBuff, sLen)

End Function


- Loren
0
 
rakhrasAuthor Commented:
It's not URLEncode that I need but rather HTMLEncode. It's not the same thing.
0
 
rakhrasAuthor Commented:
Adjusted points to 200
0
All Courses

From novice to tech pro — start learning today.