VBScript str_ireplace function

A VBScript equivalent of PHP’s str_ireplace


Function str_ireplace(search, strReplace, subject, ByRef cnt)

    If is_string(search) and isArray(strReplace) Then Exit Function

    If Not isArray(search) Then search = array(search)
    search = array_values(search)

    Dim replace_string,i
    If not isArray(strReplace) Then
        replace_string = strReplace

        strReplace = array()
        For i = 0 to uBound(search)
            [] strReplace, replace_string
        Next
    End if

    strReplace = array_values(strReplace)

    Dim length_replace,length_search
    length_replace = count(strReplace,")
    length_search  = count(search,")
    if length_replace < length_search Then
        For i = length_replace to length_search
            strReplace(i) = "
        Next
    End If

    Dim was_array : was_array = false
    If isArray(subject) Then
        was_array = true
        subject = array( subject )
    End If

    For i = 0 to uBound( search )
        search(i) = "/" & preg_quote(search(i),") & "/"
    Next

    For i = 0 to uBound( strReplace )
        strReplace(i) = str_replace( array(chr(92),"$"),array(chr(92) & chr(92), "¥$"),strReplace(i) )
    Next

    Dim result
    result = preg_replace(search,strReplace,subject,",cnt)

    If was_array = true Then
        str_ireplace = result(0)
    Else
        str_ireplace = result
    End If

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