Replace special characters with escape sequences (1 Viewer)

Tea

Member
Local time
Today, 10:01
Joined
Oct 30, 2020
Messages
51
I'm trying to find a way to replace special characters (<, >, &, ', ") in my description field with the escape sequences, so I can use the newString in a string, that is later on use to generate a xml file.

Code:
Const SpecialCharacters As String = "<,>,',&"  'modify as needed
Dim myString As String
Dim newString As String
Dim char As Variant
myString = rs![desc]
newString = myString
For Each char In Split(SpecialCharacters, ",")
    newString = Replace(newString, "<", "&lt;")
    newString = Replace(newString, ">", "&gt;")
    newString = Replace(newString, "'", "&#39;")
    newString = Replace(newString, "&", "&#38;") '#ampersand line, tried with #amp; also
Next

The problem is when I try to put the line with ampersand into the code, it gives a weird string like this:
Code:
<ItemDesc>Test &#38;#38;#38;lt; &#38;#38;#38;gt; &#38;#38;#38;#39; &#38;#38;#38;</ItemDesc>

when it should look like this.
Code:
<ItemDesc>Test < > & </ItemDesc>

With the character <, > and ' it works prefectly fine.

Is there a way to resolve this matter with a different approach?
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:01
Joined
May 7, 2009
Messages
19,247
why do you need to Replace those characters when creating an XML?
have you tried without converting them?
 

Tea

Member
Local time
Today, 10:01
Joined
Oct 30, 2020
Messages
51
Yes I did. The xml parser throws an exception and the xml file is created without the CDATA tags. So it's just a one line string without tags
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:01
Joined
Sep 21, 2011
Messages
14,336
Sub the & first?, before you put any of the replacement characters with & there?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:01
Joined
May 7, 2009
Messages
19,247
what i can see is in your code is that you are Replacing it with "&...(etc).
but later on those "&..(etc) (replacement) will be replaced Again, sample:

< will be replaced with "&alt;"
..
..
on the later part of the code this will again replaced:

"&alt;" ==> (replace the & with "&#38;") ==> &#38;alt;

*** therefore put the last part of your code to the first.

this:
Code:
Const SpecialCharacters As String = "<,>,',&"  'modify as needed
Dim myString As String
Dim newString As String
Dim char As Variant
myString = rs![desc]
newString = myString
For Each char In Split(SpecialCharacters, ",")
    newString = Replace(newString, "<", "&lt;")
    newString = Replace(newString, ">", "&gt;")
    newString = Replace(newString, "'", "&#39;")
    newString = Replace(newString, "&", "&#38;") '#ampersand line, tried with #amp; also
Next

will become:
Code:
Const SpecialCharacters As String = "<,>,',&"  'modify as needed
Dim myString As String
Dim newString As String
Dim char As Variant
myString = rs![desc]
newString = myString
For Each char In Split(SpecialCharacters, ",")
    newString = Replace(newString, "&", "&#38;") '#ampersand line, tried with #amp; also
    newString = Replace(newString, "<", "&lt;")
    newString = Replace(newString, ">", "&gt;")
    newString = Replace(newString, "'", "&#39;")
Next
 

Tea

Member
Local time
Today, 10:01
Joined
Oct 30, 2020
Messages
51
what i can see is in your code is that you are Replacing it with "&...(etc).
but later on those "&..(etc) (replacement) will be replaced Again, sample:

< will be replaced with "&alt;"
..
..
on the later part of the code this will again replaced:

"&alt;" ==> (replace the & with "&#38;") ==> &#38;alt;

*** therefore put the last part of your code to the first.

this:
Code:
Const SpecialCharacters As String = "<,>,',&"  'modify as needed
Dim myString As String
Dim newString As String
Dim char As Variant
myString = rs![desc]
newString = myString
For Each char In Split(SpecialCharacters, ",")
    newString = Replace(newString, "<", "&lt;")
    newString = Replace(newString, ">", "&gt;")
    newString = Replace(newString, "'", "&#39;")
    newString = Replace(newString, "&", "&#38;") '#ampersand line, tried with #amp; also
Next

will become:
Code:
Const SpecialCharacters As String = "<,>,',&"  'modify as needed
Dim myString As String
Dim newString As String
Dim char As Variant
myString = rs![desc]
newString = myString
For Each char In Split(SpecialCharacters, ",")
    newString = Replace(newString, "&", "&#38;") '#ampersand line, tried with #amp; also
    newString = Replace(newString, "<", "&lt;")
    newString = Replace(newString, ">", "&gt;")
    newString = Replace(newString, "'", "&#39;")
Next
I have tried that, but it's still not working. Has the same result.
Sub the & first?, before you put any of the replacement characters with & there?
What do you mean?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:01
Joined
May 7, 2009
Messages
19,247
do you have your Textfield formatted as Richtext?
on second thought, i don't know what we are doing here.
XML is different from HTM/HTML.
 

Tea

Member
Local time
Today, 10:01
Joined
Oct 30, 2020
Messages
51
do you have your Textfield formatted as Richtext?
on second thought, i don't know what we are doing here.
XML is different from HTM/HTML.
I don't thnk it's necessary to have Rich text on this field as it's not for HTML purposes. I'm generating an XML through a form. But if a field contains those special characters it does not create the xml tags becuase XML can'T read these symbols without escaping them.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:01
Joined
May 7, 2009
Messages
19,247
remove the For...Next Loop:

myString = CleanString(rs![desc])


in separate Module:

Code:
Public Function CleanString(ByVal var As Variant)
'Const SpecialCharacters As String = "<,>,',&"  'modify as needed
Dim newString As String, char As Variant
newString = var & ""
If Len(newString) = 0 Then Exit Function
'For Each char In Split(SpecialCharacters, ",")
    newString = Replace(newString, "&", "&#38;") '#ampersand line, tried with #amp; also
    newString = Replace(newString, "<", "&lt;")
    newString = Replace(newString, ">", "&gt;")
    newString = Replace(newString, "'", "&#39;")
'Next
CleanString = newString
End Function

OR on your Original Code:

Code:
'Remove this, no need
'Const SpecialCharacters As String = "<,>,',&"  'modify as needed
Dim myString As String
Dim newString As String
Dim char As Variant
myString = rs![desc]
newString = myString
'Remove For Each Loop...
'For Each char In Split(SpecialCharacters, ",")
    newString = Replace(newString, "&", "&#38;") '#ampersand line, tried with #amp; also
    newString = Replace(newString, "<", "&lt;")
    newString = Replace(newString, ">", "&gt;")
    newString = Replace(newString, "'", "&#39;")
'Next
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:01
Joined
Sep 21, 2011
Messages
14,336
Yes, but you are looping for each character, so after the first loop, you have & and we can go on an on. :(
You need to just replace them once.
I'd probably have an array of what to look for and what to replace, else use a case statement inside the loop.
 
  • Like
Reactions: Tea

Tea

Member
Local time
Today, 10:01
Joined
Oct 30, 2020
Messages
51
remove the For...Next Loop:

myString = CleanString(rs![desc])


in separate Module:

Code:
Public Function CleanString(ByVal var As Variant)
'Const SpecialCharacters As String = "<,>,',&"  'modify as needed
Dim newString As String, char As Variant
newString = var & ""
If Len(newString) = 0 Then Exit Function
'For Each char In Split(SpecialCharacters, ",")
    newString = Replace(newString, "&", "&#38;") '#ampersand line, tried with #amp; also
    newString = Replace(newString, "<", "&lt;")
    newString = Replace(newString, ">", "&gt;")
    newString = Replace(newString, "'", "&#39;")
'Next
CleanString = newString
End Function

OR on your Original Code:

Code:
'Remove this, no need
'Const SpecialCharacters As String = "<,>,',&"  'modify as needed
Dim myString As String
Dim newString As String
Dim char As Variant
myString = rs![desc]
newString = myString
'Remove For Each Loop...
'For Each char In Split(SpecialCharacters, ",")
    newString = Replace(newString, "&", "&#38;") '#ampersand line, tried with #amp; also
    newString = Replace(newString, "<", "&lt;")
    newString = Replace(newString, ">", "&gt;")
    newString = Replace(newString, "'", "&#39;")
'Next
I have remove the for each altogether, it works now.
 

Tea

Member
Local time
Today, 10:01
Joined
Oct 30, 2020
Messages
51
Yes, but you are looping for each character, so after the first loop, you have & and we can go on an on. :(
You need to just replace them once.
I'd probably have an array of what to look for and what to replace, else use a case statement inside the loop.
I have removed the for each loop, now it works fine. Made kinda sense, when the escape sequence has it's own &.
 

Tea

Member
Local time
Today, 10:01
Joined
Oct 30, 2020
Messages
51
But do you understand why it was not working?
Is there a different approach where the code would look cleaner? Is there a function that replaces especially these symbols? I would like to apply this to another field also, but it would mean I would have to repeat the same problem. If there was another & in the second string for the field [notes].

Code:
Dim myString As String
Dim newString As String
Dim myString2 As String
Dim newString2 As String
myString = rs![desc]
newString = myString
    newString = Replace(newString, "&", "&amp;")
    newString = Replace(newString, "<", "&lt;")
    newString = Replace(newString, ">", "&gt;")
    newString = Replace(newString, "'", "&#39;")
    newString = Replace(newString, Chr(34), "&#34;")
myString2 = rs![notes]
newString2 = myString2
    newString2 = Replace(newString2, "&", "&amp;")
    newString2 = Replace(newString2, "<", "&lt;")
    newString2 = Replace(newString2, ">", "&gt;")
    newString2 = Replace(newString2, "'", "&#39;")
    newString2 = Replace(newString2, Chr(34), "&#34;")
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:01
Joined
Sep 21, 2011
Messages
14,336
I have removed the for each loop, now it works fine. Made kinda sense, when the escape sequence has it's own &.
Only partially, as you were looping for each character.
If you kept the order the same then you could use the char in the loop, but then you need to know what to replace it with, hence my choice of arrays.
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:01
Joined
Sep 21, 2011
Messages
14,336
Is there a different approach where the code would look cleaner? Is there a function that replaces especially these symbols? I would like to apply this to another field also, but it would mean I would have to repeat the same problem. If there was another & in the second string for the field [notes].

Code:
Dim myString As String
Dim newString As String
Dim myString2 As String
Dim newString2 As String
myString = rs![desc]
newString = myString
    newString = Replace(newString, "&", "&amp;")
    newString = Replace(newString, "<", "&lt;")
    newString = Replace(newString, ">", "&gt;")
    newString = Replace(newString, "'", "&#39;")
    newString = Replace(newString, Chr(34), "&#34;")
myString2 = rs![notes]
newString2 = myString2
    newString2 = Replace(newString2, "&", "&amp;")
    newString2 = Replace(newString2, "<", "&lt;")
    newString2 = Replace(newString2, ">", "&gt;")
    newString2 = Replace(newString2, "'", "&#39;")
    newString2 = Replace(newString2, Chr(34), "&#34;")
Use arnel's CleanString function.
 

Users who are viewing this thread

Top Bottom