Introduction
This function is a replacement for the Server.HTMLEncode
method found in Classic ASP with one major difference... It accepts null
strings without throwing errors!
The side effect is HTML Encoding for VBScript.
Background
I wrote this to overcome the common IsNull
, IsNothing
, IsEmpty string
nightmare experienced when calling Server.HTMLEncode
from Classic ASP.
Using the Code
Function HTMLEncode(ByVal sVal)
sReturn = ""
If ((TypeName(sVal)="String") And (Not IsNull(sVal)) And (sVal<>"")) Then
For i = 1 To Len(sVal)
ch = Mid(sVal, i, 1)
Set oRE = New RegExp : oRE.Pattern = "[ a-zA-Z0-9]"
If (Not oRE.Test(ch)) Then
ch = "&#" & Asc(ch) & ";"
End If
sReturn = sReturn & ch
Set oRE = Nothing
Next
End If
HTMLEncode = sReturn
End Function
HTMLEncode("This is a & test!")
History
- 4th February, 2009: Initial post