2

How can I pass the data generated by the EliminaAcentos function to the URLEncode function in this script?

The first function removes diacritics and the second function URL-encodes the data.

Function EliminaAcentos(texto)
    Dim i, s1, s2
    s1 = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜàáâãäåçèéêëìíîïòóôõöùúûü"
    s2 = "AAAAAACEEEEIIIIOOOOOUUUUaaaaaaceeeeiiiiooooouuuu"
    If Len(texto) <> 0 Then
        For i = 1 To Len(s1)
            texto = Replace(texto, Mid(s1,i,1), Mid(s2,i,1))
        Next
    End If

    EliminaAcentos = texto
End Function

Function URLEncode(ByVal str)
    Dim strTemp, strChar
    Dim intPos, intASCII
    strTemp = ""
    strChar = ""

    For intPos = 1 To Len(str)
        intASCII = Asc(Mid(str, intPos, 1))
        If intASCII = 32 Then
            strTemp = strTemp & "+"
        ElseIf ((intASCII < 123) And (intASCII > 96)) Then
            strTemp = strTemp & Chr(intASCII)
        ElseIf ((intASCII < 91) And (intASCII > 64)) Then
            strTemp = strTemp & Chr(intASCII)
        ElseIf ((intASCII < 58) And (intASCII > 47)) Then
            strTemp = strTemp & Chr(intASCII)
        Else
            strChar = Trim(Hex(intASCII))
            If intASCII < 16 Then
                strTemp = strTemp & "%0" & strChar
            Else
                strTemp = strTemp & "%" & strChar
            End If
        End If
    Next

    URLEncode = strTemp
End Function

WScript.Echo URLEncode(WScript.Arguments(0))
3
  • 1
    URLEncode( EliminaAcentos( WScript.Arguments(0))) but ensure that EliminaAcentos function declaration should be Function EliminaAcentos( ByVal texto) Commented Jun 19, 2016 at 20:33
  • I put this in the end of the script, but is not working (I did the ByVal texto) too. Wscript.Echo URLEncode( EliminaAcentos( WScript.Arguments(0))) Commented Jun 19, 2016 at 21:36
  • 1
    Define "not working". Commented Jun 19, 2016 at 22:26

1 Answer 1

1

Basically there are two ways to go about it:

  • You can nest the call of EliminaAcentos in the call of URLEncode as @JosefZ suggested:

    URLEncode(EliminaAcentos(WScript.Arguments(0)))
    
  • You can embed the call of EleminaAcentos in the body of the URLEncode function:

    Function URLEncode(ByVal str)
        Dim strTemp, strChar
        Dim intPos, intASCII
        strTemp = ""
        strChar = ""
    
        str = EliminaAcentos(str)
        For intPos = 1 To Len(str)
            ...
        Next
    
        URLEncode = strTemp
    End Function
    

Usually you'd pick the first option if there are situations where you call URLEncode and don't want diacritics removed, or if you don't control the implementation of URLEncode. If you always want URLEncode to remove diacritics and you control the function implementation you'd pick the second option.


Side note (also mentioned by @JosefZ): pass the parameter to EliminaAcentos by value, so the function call doesn't inadvertently modify the original value.

Function EliminaAcentos(ByVal texto)
    ...
End Function
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for your help ! I´m using your second example (embed call), and it´s working fine :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.