Replace a function, how to replace not all found strings?

I am creating an XML file from an excel file. I save a row from one cell in the sObserved variable. When I have a symbol in this line "&", it should be replaced by "&", and when I have a symbol ";", it should be replaced by "&#59;". I use Replace for this function, this is my code:

        sObserved = Replace(sObserved, "&", "&")
        sObserved = Replace(sObserved, ";", "&#59;")

But this will not work, because when it replaces "&"with "&", it will appear ";", and the next operation will change it to "&&#59;" If I change the order, it will also be wrong, because then sign "&" in ";"will be replaced.

Is there any way to replace it the way I wanted? I will be grateful for any ideas because I am stuck here.

+4
source share
3 answers

Try the following:

sObserved = Replace(sObserved, "&", "&")
sObserved = Replace(sObserved, ";", "&#59;")
sObserved = Replace(sObserved, "&amp&#59;", "&")
+2
source

This function is from http://www.codeproject.com/Articles/33064/VBScript-HTML-Encode . You may need to twist it, but it performs a character check with a character.

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
+1
source

, , - , HTML: htmlEncode. /, , :

' Encode an string so that it can be displayed correctly
' inside the browser.
'
' Same effect as the Server.HTMLEncode method in ASP

Function HTMLEncode(ByVal Text As String) As String
Dim i As Integer
Dim acode As Integer
Dim repl As String

HTMLEncode = Text

For i = Len(HTMLEncode) To 1 Step -1
    acode = Asc(Mid$(HTMLEncode, i, 1))
    Select Case acode
        Case 32
            repl = "&nbsp;"
        Case 34
            repl = "&quot;"
        Case 38
            repl = "&amp;"
        Case 60
            repl = "&lt;"
        Case 62
            repl = "&gt;"
        Case 32 To 127
            ' don't touch alphanumeric chars
        Case Else
            repl = "&#" & CStr(acode) & ";"
    End Select
    If Len(repl) Then
        HTMLEncode = Left$(HTMLEncode, i - 1) & repl & Mid$(HTMLEncode, _
            i + 1)
        repl = ""
    End If
Next
End Function

ref: http://www.devx.com/vb2themax/Tip/19162

: http://www.codeproject.com/Articles/33064/VBScript-HTML-Encode , , , . , , , , HTML.

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
+1
source

Source: https://habr.com/ru/post/1620301/


All Articles