forked from nuovo/spreadsheet-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpreadsheetReader_CSV.php
274 lines (245 loc) · 6.79 KB
/
SpreadsheetReader_CSV.php
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
* Class for parsing CSV files
*
* @author Martins Pilsetnieks
*/
class SpreadsheetReader_CSV implements Iterator, Countable
{
/**
* @var array Options array, pre-populated with the default values.
*/
private $Options = array(
'Delimiter' => ';',
'Enclosure' => '"'
);
private $Encoding = 'UTF-8';
private $BOMLength = 0;
/**
* @var resource File handle
*/
private $Handle = false;
private $Filepath = '';
private $Index = 0;
private $CurrentRow = array();
/**
* @param string Path to file
* @param array Options:
* Enclosure => string CSV enclosure
* Separator => string CSV separator
*/
public function __construct($Filepath, array $Options = null)
{
$this -> Filepath = $Filepath;
if (!is_readable($Filepath))
{
throw new Exception('SpreadsheetReader_CSV: File not readable ('.$Filepath.')');
}
// For safety's sake
@ini_set('auto_detect_line_endings', true);
$this -> Options = array_merge($this -> Options, $Options);
$this -> Handle = fopen($Filepath, 'r');
// Checking the file for byte-order mark to determine encoding
$BOM16 = bin2hex(fread($this -> Handle, 2));
if ($BOM16 == 'fffe')
{
$this -> Encoding = 'UTF-16LE';
//$this -> Encoding = 'UTF-16';
$this -> BOMLength = 2;
}
elseif ($BOM16 == 'feff')
{
$this -> Encoding = 'UTF-16BE';
//$this -> Encoding = 'UTF-16';
$this -> BOMLength = 2;
}
if (!$this -> BOMLength)
{
fseek($this -> Handle, 0);
$BOM32 = bin2hex(fread($this -> Handle, 4));
if ($BOM32 == '0000feff')
{
//$this -> Encoding = 'UTF-32BE';
$this -> Encoding = 'UTF-32';
$this -> BOMLength = 4;
}
elseif ($BOM32 == 'fffe0000')
{
//$this -> Encoding = 'UTF-32LE';
$this -> Encoding = 'UTF-32';
$this -> BOMLength = 4;
}
}
fseek($this -> Handle, 0);
$BOM8 = bin2hex(fread($this -> Handle, 3));
if ($BOM8 == 'efbbbf')
{
$this -> Encoding = 'UTF-8';
$this -> BOMLength = 3;
}
// Seeking the place right after BOM as the start of the real content
if ($this -> BOMLength)
{
fseek($this -> Handle, $this -> BOMLength);
}
// Checking for the delimiter if it should be determined automatically
if (!$this -> Options['Delimiter'])
{
// fgetcsv needs single-byte separators
$Semicolon = ';';
$Tab = "\t";
$Comma = ',';
// Reading the first row and checking if a specific separator character
// has more columns than others (it means that most likely that is the delimiter).
$SemicolonCount = count(fgetcsv($this -> Handle, null, $Semicolon));
fseek($this -> Handle, $this -> BOMLength);
$TabCount = count(fgetcsv($this -> Handle, null, $Tab));
fseek($this -> Handle, $this -> BOMLength);
$CommaCount = count(fgetcsv($this -> Handle, null, $Comma));
fseek($this -> Handle, $this -> BOMLength);
$Delimiter = $Semicolon;
if ($TabCount > $SemicolonCount || $CommaCount > $SemicolonCount)
{
$Delimiter = $CommaCount > $TabCount ? $Comma : $Tab;
}
$this -> Options['Delimiter'] = $Delimiter;
}
}
/**
* Returns information about sheets in the file.
* Because CSV doesn't have any, it's just a single entry.
*
* @return array Sheet data
*/
public function Sheets()
{
return array(0 => basename($this -> Filepath));
}
/**
* Changes sheet to another. Because CSV doesn't have any sheets
* it just rewinds the file so the behaviour is compatible with other
* sheet readers. (If an invalid index is given, it doesn't do anything.)
*
* @param bool Status
*/
public function ChangeSheet($Index)
{
if ($Index == 0)
{
$this -> rewind();
return true;
}
return false;
}
// !Iterator interface methods
/**
* Rewind the Iterator to the first element.
* Similar to the reset() function for arrays in PHP
*/
public function rewind()
{
fseek($this -> Handle, $this -> BOMLength);
$this -> Index = 0;
}
/**
* Return the current element.
* Similar to the current() function for arrays in PHP
*
* @return mixed current element from the collection
*/
public function current()
{
if ($this -> Index == 0)
{
$this -> next();
$this -> Index--;
}
return $this -> CurrentRow;
}
/**
* Move forward to next element.
* Similar to the next() function for arrays in PHP
*/
public function next()
{
// Finding the place the next line starts for UTF-16 encoded files
// Line breaks could be 0x0D 0x00 0x0A 0x00 and PHP could split lines on the
// first or the second linebreak leaving unnecessary \0 characters that mess up
// the output.
if ($this -> Encoding == 'UTF-16LE' || $this -> Encoding == 'UTF-16BE')
{
while (!feof($this -> Handle))
{
// While bytes are insignificant whitespace, do nothing
$Char = ord(fgetc($this -> Handle));
if (!$Char || $Char == 10 || $Char == 13)
{
continue;
}
else
{
// When significant bytes are found, step back to the last place before them
if ($this -> Encoding == 'UTF-16LE')
{
fseek($this -> Handle, ftell($this -> Handle) - 1);
}
else
{
fseek($this -> Handle, ftell($this -> Handle) - 2);
}
break;
}
}
}
$this -> Index++;
$this -> CurrentRow = fgetcsv($this -> Handle, null, $this -> Options['Delimiter'], $this -> Options['Enclosure']);
if ($this -> CurrentRow)
{
// Converting multi-byte unicode strings
// and trimming enclosure symbols off of them because those aren't recognized
// in the relevan encodings.
if ($this -> Encoding != 'ASCII' && $this -> Encoding != 'UTF-8')
{
$Encoding = $this -> Encoding;
foreach ($this -> CurrentRow as $Key => $Value)
{
$this -> CurrentRow[$Key] = trim(trim(
mb_convert_encoding($Value, 'UTF-8', $this -> Encoding),
$this -> Options['Enclosure']
));
}
}
}
return $this -> CurrentRow;
}
/**
* Return the identifying key of the current element.
* Similar to the key() function for arrays in PHP
*
* @return mixed either an integer or a string
*/
public function key()
{
return $this -> Index;
}
/**
* Check if there is a current element after calls to rewind() or next().
* Used to check if we've iterated to the end of the collection
*
* @return boolean FALSE if there's nothing more to iterate over
*/
public function valid()
{
return ($this -> CurrentRow || !feof($this -> Handle));
}
// !Countable interface method
/**
* Ostensibly should return the count of the contained items but this just returns the number
* of rows read so far. It's not really correct but at least coherent.
*/
public function count()
{
return $this -> Index + 1;
}
}
?>