VBScript substr_replace function

A VBScript equivalent of PHP’s substr_replace


Function substr_replace(str, replacement, start, length)

    Dim key

    If isArray(str) Then
        For key = 0 to uBound(str)
            substr_replace(key) = substr_replace(str(key), replacement, start, length)
        Next
        Exit Function
    ElseIf isObject(str) Then
        For Each key In str
            substr_replace(key) = substr_replace(str(key), replacement, start, length)
        Next
        Exit Function
    End If

    Dim result

    If start < 0 Then
        start = len(str) + start
    End If

    If start <> 0 Then
        result = Left(str,start)
    End If

    result = result & replacement

    If len(length) > 0 Then
        If length > 0 Then
            result = result & Mid(str,start + length)
        Else
            result = result & Right(str,abs(length))
        End If

    End If

    substr_replace = result
End Function

Please also note that php.vbs offers community built functions and goes by the McDonald’s Theory. We’ll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just:

Other PHP functions in the strings extension