csv-bestanden-parsen

Gesponsorde koppelingen

PHP script bestanden

  1. csv-bestanden-parsen

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php

    // Parse a CSV file to a 2D array.
    function csvToArray($sFilePath = "", $bSkipEmptyLines = true, $bReportErrors = true)
    {

        /*
            - escape quote: "" en/of \"                    Alleen de Micro$oft methode is opgenomen => ""
            - seperator: ';' en/of ','                    Zowel ; als , zijn geldige scheidingstekens
            - spaties in document wel/niet overnemen?    Als waarden ge-quote zijn, worden deze niet getrimmed, anders worden de waarden wel getrimmed.
            - lege regels wel/niet opnemen in array?    Configureerbaar d.b.v. de boolean $bSkipEmptyLines.
            - velden met enters?                        Als waarden ge-quote zijn, mogen deze enters bevatten.
        */


        $aSeperators = array(",", ";");
        $iLineCount = 0;

        $aRows = array();
        $aColumns = array();

        if(strcasecmp($sFilePath, "") !== 0)
        {

            if(is_file($sFilePath))
            {

                if(filesize($sFilePath) > 0)
                {

                    if(is_readable($sFilePath))
                    {

                        $sContent = file_get_contents($sFilePath);
                        $sValue = '';

                        for($i = 0; $i < strlen($sContent); $i++)
                        {

                            $sCharacter = substr($sContent, $i, 1);

                            if(strcasecmp($sCharacter, "\"") === 0) // Quoted string starts
                            {
                                // Append to string untill closing quote found
                                $bValueClosed = false;
                                $sValue = '';

                                while($bValueClosed === false)
                                {

                                    $i++;

                                    if((strlen($sContent) - 1) < $i) // EOF
                                    {
                                        $bValueClosed = true;

                                        $aColumns[] = $sValue; // Save current column
                                        $aRows[] = $aColumns; // Save current row
                                    }
                                    else
                                    {
                                        $sCharacter = substr($sContent, $i, 1);

                                        if(strcasecmp($sCharacter, "\"") === 0)
                                        {

                                            if((strlen($sContent) - 1) < ($i + 1)) // EOF
                                            {
                                                $bValueClosed = true;

                                                $aColumns[] = $sValue; // Save current column
                                                $aRows[] = $aColumns; // Save current row
                                                $aColumns = array();
                                            }

                                            else
                                            {
                                                // Check for "" (Microsoft quote support).
                                                $sCharacterNext = substr($sContent, $i + 1, 1);
                                                if(strcasecmp($sCharacterNext, "\"") === 0)
                                                {

                                                    $sValue .= '"';
                                                    $i++;
                                                }

                                                else
                                                {
                                                    $bValueClosed = true;
                                                    $aColumns[] = $sValue; // Save current column

                                                    $i++;

                                                    // Remove characters after the quote, probably characters such as spaces/tabs/etc.
                                                    $sCharacterNext = substr($sContent, $i, 1); // Lookup next seperator sign or linebreak.
                                                    while((in_array($sCharacterNext, $aSeperators) === false) && (strcasecmp($sCharacterNext, "\n") !== 0))
                                                    {

                                                        $i++;
                                                        $sCharacterNext = substr($sContent, $i, 1);
                                                    }


                                                    if(strcasecmp($sCharacterNext, "\n") === 0)
                                                    {

                                                        $aRows[] = $aColumns; // Save current row
                                                        $aColumns = array();
                                                    }
                                                }
                                            }
                                        }

                                        else
                                        {
                                            $sValue .= $sCharacter;
                                        }
                                    }
                                }


                                $sValue = '';
                            }

                            elseif(in_array($sCharacter, $aSeperators)) // Column seperator character
                            {
                                $aColumns[] = trim($sValue); // Save current column
                                $sValue = '';
                            }

                            elseif(strcasecmp($sCharacter, "\n") === 0) // New line character
                            {
                                if($bSkipEmptyLines && ((strcasecmp($sValue, "") === 0) || (strcasecmp($sValue, "\r") === 0)) && (sizeof($aColumns) === 0))
                                {

                                    // Ignore line
                                    $sValue = '';
                                    $iLineCount++;
                                }

                                else
                                {
                                    $aColumns[] = trim($sValue); // Save current column
                                    $aRows[] = $aColumns; // Save current row

                                    $aColumns = array();
                                    $sValue = '';

                                    $iLineCount++;
                                }
                            }

                            elseif(strcasecmp($sCharacter, "\r") !== 0)
                            {

                                $sValue .= $sCharacter;
                            }
                        }
                    }

                    else
                    {
                        if($bReportErrors)
                        {

                            echo 'File ' . htmlentities($sFilePath) . ' is not readable. FILE: ' . __FILE__ . ', LINE: ' . __LINE__;
                        }
                    }
                }

                else
                {
                    if($bReportErrors)
                    {

                        echo 'File ' . htmlentities($sFilePath) . ' has no data. FILE: ' . __FILE__ . ', LINE: ' . __LINE__;
                    }
                }
            }

            else
            {
                if($bReportErrors)
                {

                    echo 'File ' . htmlentities($sFilePath) . ' does not exists. FILE: ' . __FILE__ . ', LINE: ' . __LINE__;
                }
            }
        }

        else
        {
            if($bReportErrors)
            {

                echo 'No filepath given. FILE: ' . __FILE__ . ', LINE: ' . __LINE__;
            }
        }


        return $aRows;
    }


?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.