Hi all,
I am trying to replace html characters with their encoded entities. More specifically I am trying to replace " and & with their encoded entity " and &.
The problem is that the character & is a special sequence and is replaced with the string that matches the pattern (i.e. replacing a " will return this encoding "quot; instead of ". I would like for the proc to return the proper encoding of the character and unfortunately this has proven quite difficult. Any help in this subject matter or perhaps another way of doing this would be great, TIA.
Here is my test code:
[
set MyContent1 {this is "1" & 2}
proc HTML_ENCODE {inString} {
regsub -all {&} $inString {&} outString
regsub -all {"} $outString {"} outString
# since the ampersand will be encoded incorrectly
#we need to regsub it again, *ungh*
regsub -all {amp;amp;} $outString {amp;} outString
return $outString
}
proc HTML_DECODE {inString} {
regsub -all {&} $inString {&} outString
regsub -all {"} $outString {"} outString
return $outString
}
]
Encode:
[set MyEncode1 [HTML_ENCODE $MyContent1]]
<br>
Decode:
[set MyContent1 [HTML_DECODE $MyEncode1]]
<script language=javascript>
alert ('[HTML_ENCODE $MyContent1]');
</script>
I am trying to replace html characters with their encoded entities. More specifically I am trying to replace " and & with their encoded entity " and &.
The problem is that the character & is a special sequence and is replaced with the string that matches the pattern (i.e. replacing a " will return this encoding "quot; instead of ". I would like for the proc to return the proper encoding of the character and unfortunately this has proven quite difficult. Any help in this subject matter or perhaps another way of doing this would be great, TIA.
Here is my test code:
[
set MyContent1 {this is "1" & 2}
proc HTML_ENCODE {inString} {
regsub -all {&} $inString {&} outString
regsub -all {"} $outString {"} outString
# since the ampersand will be encoded incorrectly
#we need to regsub it again, *ungh*
regsub -all {amp;amp;} $outString {amp;} outString
return $outString
}
proc HTML_DECODE {inString} {
regsub -all {&} $inString {&} outString
regsub -all {"} $outString {"} outString
return $outString
}
]
Encode:
[set MyEncode1 [HTML_ENCODE $MyContent1]]
<br>
Decode:
[set MyContent1 [HTML_DECODE $MyEncode1]]
<script language=javascript>
alert ('[HTML_ENCODE $MyContent1]');
</script>