udfHtmlEncode
str udfHtmlEncode (str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfHtmlEncode (strText)
If strText == "" Then Return strText
objRE = ObjectCreate ("VBScript.RegExp")
objRE.IgnoreCase = @TRUE
objRE.Global = @TRUE
objRE.MultiLine = @TRUE
objRE.Pattern = "[^\w ]" ; Leave word class characters and space untouched.
; objRE.Pattern = "[^0-9 A-Z]" ; Leave numbers and space and letters A-Z untouched.
; objRE.Pattern = "[^\x20-\x21\x28-\x3B\x41-\x5A\x61-\x7A]" ; Leave numbers, letters and some other characters untouched.
; objRE.Pattern = "[\x00-\x1F\x7F-\xFF\x3C\x3E\x22\x26]" ; Encode control chars, high-bit chars and the '<', '>', '"' and '&' character.
arrChars = ArrayFromStr (strText)
intLast = ArrInfo (arrChars, 1) - 1
For intI = 0 To intLast
   If objRE.Test(arrChars[intI]) Then arrChars[intI] = "&#" : Char2Num (arrChars[intI]) : ";"
Next
Return ArrayToStr (arrChars)
;..........................................................................................................................................
; This UDF "udfHtmlEncode" converts some characters to their "Decimal numerical character reference" HTML entity
; for safe usage within HTML script.
;
; The encoded replacement is a character sequence of leading "&#" plus one or more digits in the range 0–9 plus trailing ";".
; Example: "<" will be encoded to "&#60;".
;
; The standard set of characters to encode contain control chars, high-bit chars and the '<', '&', '>' and '"' character.
;
; (c)Detlev Dalitz.20120107.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


DirChange (DirScript ())

; Test1.

strText = "This is a test: " : @CRLF : " 123 ! < > & ; ä ö ü ß - + ~ " : @TAB : " The End ."
strTextEncoded = udfHtmlEncode (strText)
strMsgTitle = "Test.1.udfHtmlEncode"
strMsgText = strText : @LF : @LF : strTextEncoded
Pause (strMsgTitle, strMsgText)
;------------------------------------------------------------------------------------------------------------------------------------------
; Examples:
;
; objRE.Pattern = "[^\w ]" ; Leave word class characters and space untouched.
; "This is a test&#58; &#13;&#10; 123 &#33; &#60; &#62; &#38; &#59; &#228; &#246; &#252; &#223; &#45; &#43; &#126; &#9; The End &#46;"
;
; objRE.Pattern = "[^0-9 A-Z]" ; Leave numbers and space and letters A-Z untouched.
; "This is a test&#58; &#13;&#10; 123 &#33; &#60; &#62; &#38; &#59; &#228; &#246; &#252; &#223; &#45; &#43; &#126; &#9; The End &#46;"
;
; objRE.Pattern = "[^\x20-\x21\x28-\x3B\x41-\x5A\x61-\x7A]" ; Leave numbers, letters and some other characters untouched.
; "This is a test: &#13;&#10; 123 ! &#60; &#62; &#38; ; &#228; &#246; &#252; &#223; - + &#126; &#9; The End ."
;
; objRE.Pattern = "[\x00-\x1F\x7F-\xFF\x3C\x3E\x22\x26]" ; Encode control chars, high-bit chars and the '<', '>', '"' and '&' character.
; "This is a test: &#13;&#10; 123 ! &#60; &#62; &#38; ; &#228; &#246; &#252; &#223; - + ~ &#9; The End ."
;------------------------------------------------------------------------------------------------------------------------------------------


; Test2.
;   ;------------------------------------------------------------------------------------------------------------------------------------------
;   #DefineFunction udfByteToHexV1 (intByte) ; intByte = 0..255.
;   strHexDigits = "0123456789ABCDEF" ; strHexDigits = "0123456789abcdef".
;   Return StrSub (strHexDigits, 1 + (intByte >> 4), 1) : StrSub (strHexDigits, 1 + (intByte & 15), 1)
;   #EndFunction
;   ;------------------------------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfByteToHexV4 (intByte) ; intByte = 0..255.
arrHD = ArrayFromStr ("0123456789ABCDEF") ; Hexadecimal digits.
Return arrHD[intByte >> 4] : arrHD[intByte & 15]
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

strText = ""
For intI = 1 To 255
   strCharIn = Num2Char (intI)
   strCharOut = udfHtmlEncode (strCharIn)
   strText = strText : @CRLF : "d" : StrFixLeft (intI, "0", 3) : " | x" : udfByteToHexV4 (intI) : " | " : strCharIn : " | " : strCharOut
Next

strFileOut = Environment ("TEMP") : "\Test.2.udfHtmlEncode.txt"
intBytesWritten = FilePut (strFileOut, "Test.2.udfHtmlEncode" : @CRLF : strText)
blnResult = Run (strFileOut, "")
If 1 == FileExist (strFileOut) Then FileDelete (strFileOut)

:CANCEL
Exit