Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

ezcsvimport.php
Go to the documentation of this file.
1 <?PHP
32 class eZCSVImport {
33
34
38 function eZCSVImport() {
39 }
40
41
48 function csvFileToArray($fullPath, $separator=';', $trim='none', $removeHeader=FALSE, $removeEmptyLines=FALSE, $checkMultiline=FALSE) {
49 $fileContent = @file($fullPath);
50 if (!$fileContent) return FALSE;
51
52 //hrm, having similar prob as in csvStringToArray() here except this time i need it for \n not \r.
53 //so let's remove that aswell ... --andrej
54 while (list($k) = each($fileContent)) {
55 if ((substr($fileContent[$k], -1) == "\r") || (substr($fileContent[$k], -1) == "\n")) {
56 $fileContent[$k] = substr($fileContent[$k], 0, -1);
57 }
58 }
59 reset($fileContent);
60
61 if ($checkMultiline) $fileContent = $this->_checkMultiline($fileContent);
62 return $this->csvArrayToArray($fileContent, $separator, $trim, $removeHeader, $removeEmptyLines);
63 }
64
65
72 function csvStringToArray($string, $separator=';', $trim='none', $removeHeader=FALSE, $removeEmptyLines=FALSE, $checkMultiline=FALSE) {
73 if (empty($string)) return array();
74 $array = explode("\n", $string);
75
76 //short hack: on windows we should explode by "\r\n". if not, the elements in $array still end with \r.
77 //so let's remove that ... --andrej
78 while (list($k) = each($array)) {
79 if (substr($array[$k], -1) == "\r") {
80 $array[$k] = substr($array[$k], 0, -1);
81 }
82 }
83 reset($array);
84
85 if ((!is_array($array)) || empty($array)) return array();
86 if ($checkMultiline) $array = $this->_checkMultiline($array);
87 return $this->csvArrayToArray($array, $separator, $trim, $removeHeader, $removeEmptyLines);
88 }
89
90
121 function csvArrayToArray($array, $separator=';', $trim='none', $removeHeader=FALSE, $removeEmptyLines=FALSE) {
122 switch ($trim) {
123 case 'none':
124 $trimFunction = FALSE;
125 break;
126 case 'left':
127 $trimFunction = 'ltrim';
128 break;
129 case 'right':
130 $trimFunction = 'rtrim';
131 break;
132 default: //'both':
133 $trimFunction = 'trim';
134 break;
135 }
136
137 $sepLength = strlen($separator);
138
139 if ($removeHeader) {
140 array_shift($array);
141 }
142
143 $ret = array();
144 reset($array);
145 while (list(,$line) = each($array)) {
146 $offset = 0;
147 $lastPos = 0;
148 $lineArray = array();
149 do {
150 //find the next separator
151 $pos = strpos($line, $separator, $offset);
152 if ($pos === FALSE) {
153 //no more separators.
154 $lineArray[] = substr($line, $lastPos);
155 break;
156 }
157 //now let's see if it is inside a field value (text) or it is a real separator.
158 //it can only be a separator if the number of quotes (") since the last separator
159 //is straight (not odd).
160 $currentSnippet = substr($line, $lastPos, $pos-$lastPos);
161 $numQuotes = substr_count($currentSnippet, '"');
162 if ($numQuotes % 2 == 0) {
163 //that's good, we got the next field. the separator was a real one.
164 $lineArray[] = substr($line, $lastPos, $pos-$lastPos);
165 $lastPos = $pos + $sepLength;
166 } else {
167 //have to go on, separator was inside a field value.
168 }
169 $offset = $pos + $sepLength;
170 } while (TRUE);
171
172 //trim if needed
173 if ($trimFunction !== FALSE) {
174 while (list($k) = each($lineArray)) {
175 $lineArray[$k] = $trimFunction($lineArray[$k]);
176 }
177 reset($lineArray);
178 }
179
180 //remove quotes around cell values, and unescape other quotes.
181 while (list($k) = each($lineArray)) {
182 if ((substr($lineArray[$k], 0, 1) == '"') && (substr($lineArray[$k], 1, 1) != '"') && (substr($lineArray[$k], -1) == '"')) {
183 //string has to look like "hello world" and may not look like ""hello.
184 //if two quotes are together, it's an escaped one. csv uses ms-escape style.
185 $lineArray[$k] = substr($lineArray[$k], 1, -1);
186 }
187 //now un-escape the other quotes
188 $lineArray[$k] = str_replace('""', '"', $lineArray[$k]);
189 }
190 reset($lineArray);
191
192 //removeEmptyLines
193 $addIt = TRUE;
194 if ($removeEmptyLines) {
195 do {
196 while (list($k) = each($lineArray)) {
197 if (!empty($lineArray[$k])) break 2;
198 }
199 $addIt = FALSE;
200 } while (FALSE);
201 reset($lineArray);
202 }
203
204 if ($addIt) {
205 $ret[] = $lineArray;
206 }
207 }
208
209 return $ret;
210 }
211
212
239 function arrayToCsvString($array, $separator=';', $trim='none', $removeEmptyLines=TRUE) {
240 if (!is_array($array) || empty($array)) return '';
241
242 switch ($trim) {
243 case 'none':
244 $trimFunction = FALSE;
245 break;
246 case 'left':
247 $trimFunction = 'ltrim';
248 break;
249 case 'right':
250 $trimFunction = 'rtrim';
251 break;
252 default: //'both':
253 $trimFunction = 'trim';
254 break;
255 }
256
257 $ret = array();
258 reset($array);
259 if (is_array(current($array))) {
260 while (list(,$lineArr) = each($array)) {
261 if (!is_array($lineArr)) {
262 //could issue a warning ...
263 $ret[] = array();
264 } else {
265 $subArr = array();
266 while (list(,$val) = each($lineArr)) {
267 $val = $this->_valToCsvHelper($val, $separator, $trimFunction);
268 $subArr[] = $val;
269 }
270 }
271 $ret[] = join($separator, $subArr);
272 }
273 return join("\n", $ret);
274 } else {
275 while (list(,$val) = each($array)) {
276 $val = $this->_valToCsvHelper($val, $separator, $trimFunction);
277 $ret[] = $val;
278 }
279 return join($separator, $ret);
280 }
281 }
282
283
293 function _valToCsvHelper($val, $separator, $trimFunction) {
294 if ($trimFunction) $val = $trimFunction($val);
295 //if there is a separator (;) or a quote (") or a linebreak in the string, we need to quote it.
296 $needQuote = FALSE;
297 do {
298 if (strpos($val, '"') !== FALSE) {
299 $val = str_replace('"', '""', $val);
300 $needQuote = TRUE;
301 break;
302 }
303 if (strpos($val, $separator) !== FALSE) {
304 $needQuote = TRUE;
305 break;
306 }
307 if ((strpos($val, "\n") !== FALSE) || (strpos($val, "\r") !== FALSE)) { // \r is for mac
308 $needQuote = TRUE;
309 break;
310 }
311 } while (FALSE);
312 if ($needQuote) {
313 $val = '"' . $val . '"';
314 }
315 return $val;
316 }
317
318
319
326 function _checkMultiline($in) {
327 $ret = array();
328
329 $stack = FALSE;
330 reset($in);
331 while (list(,$line) = each($in)) {
332 $c = substr_count($line, '"');
333 if ($c % 2 == 0) {
334 if ($stack === FALSE) {
335 $ret[] = $line;
336 } else {
337 $stack .= "\n" . $line;
338 }
339 } else {
340 //odd number
341 if ($stack === FALSE) {
342 $stack = $line;
343 } else {
344 $ret[] = $stack . "\n" . $line;
345 $stack = FALSE;
346 }
347 }
348 }
349 return $ret;
350 }
351
352 } // end Class
353
354 ?>
eZCSVImport
Definition: ezcsvimport.php:32
eZCSVImport\_valToCsvHelper
_valToCsvHelper($val, $separator, $trimFunction)
Definition: ezcsvimport.php:293
eZCSVImport\arrayToCsvString
arrayToCsvString($array, $separator=';', $trim='none', $removeEmptyLines=TRUE)
Definition: ezcsvimport.php:239
eZCSVImport\csvStringToArray
csvStringToArray($string, $separator=';', $trim='none', $removeHeader=FALSE, $removeEmptyLines=FALSE, $checkMultiline=FALSE)
Definition: ezcsvimport.php:72
eZCSVImport\eZCSVImport
eZCSVImport()
Definition: ezcsvimport.php:38
eZCSVImport\csvFileToArray
csvFileToArray($fullPath, $separator=';', $trim='none', $removeHeader=FALSE, $removeEmptyLines=FALSE, $checkMultiline=FALSE)
Definition: ezcsvimport.php:48
eZCSVImport\_checkMultiline
_checkMultiline($in)
Definition: ezcsvimport.php:326
eZCSVImport\csvArrayToArray
csvArrayToArray($array, $separator=';', $trim='none', $removeHeader=FALSE, $removeEmptyLines=FALSE)
Definition: ezcsvimport.php:121
$separator
else $separator
Definition: cron_alerts.php:52
$ret
$ret
Definition: ezsitemanager/admin/filelist.php:88
$offset
$offset
Definition: ezstats/admin/cron.php:51
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.