VBScript substr function

A VBScript equivalent of PHP’s substr


Function substr(ByVal str,ByVal start,ByVal intLength)

	intStart = start
    If len(intStart) = 0 Then intStart = 0
    if intStart = 0 And Len(intLength) < 1 Then
    	substr = str
    	Exit Function
    End If
    
    If len(intLength) = 0 Then intLength = abs(start)
    
    If intStart < 0 Then
        intStart = len(str)+1 + intStart
    Else
    	intStart = intStart + 1
    End If

    Dim tmp
    If intStart <> 0 Then
	    tmp = Mid(str,intStart)
	Else 
		tmp = str
	End If

    Dim intLen
    intLen = len(tmp)


    If len(intLength) > 0 Then

        If intLen >= abs(intLength) Then
            If intLength > 0 Then

        		tmp = Left(tmp,intLength)

            Else
                tmp = Left(tmp,len(tmp) + intLength)
            End If
        Else
            tmp = False
        End If
    End If

    substr = tmp

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