VBScript fgetcsv function

A VBScript equivalent of PHP’s fgetcsv


Public Function fgetcsv(delim)

    Dim tmp,d
    If len(delim) > 0 Then d = delim Else d = "",""
    tmp = ts.ReadLine
    fgetcsv = fgetcsv_helper(tmp,d)

End Function


Public Function fgetcsv_helper(str,d)

    Dim matchAll,key
    Dim data,field,record : field = 0 : record = 0
    ReDim data(0)

    If preg_match_all(_
    ""/"" & d & ""|"" & vbCr & ""?"" & vbLf & ""|[^"" & d & """" & vbCrLf & ""][^"" & d & "" & vbCrLf & ""]*|""(?:[^""]|"""")*""/"",_
    str, matchAll,PREG_PATTERN_ORDER,"") Then
        For Each key In matchAll(0)
            Select Case key
            Case d
                field = field + 1
            Case vbCrLf
                [] data , ""
                record = record +1
            Case Else
                If left(key,1) = """" Then
                    key = Replace(substr(key,2,-1),"""","""")
                End if
                [] data(record), key
            End Select
        Next
    End If

    fgetcsv_helper = data

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 filesystem extension