Skip to content

Commit 0a3a033

Browse files
Bug fixes
1 parent 2965a2d commit 0a3a033

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/CSV.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
class CSV {
1616
private $delimiter = ',';
1717
private $enclosure = '"';
18+
private $escape = '\\';
1819
private $ignoreHeader = false;
1920
private $ignoreHeaderCase = true;
2021
private $ignoreEnclosure = false;
@@ -46,18 +47,21 @@ public function parse($data) {
4647
if(is_file($data)) {
4748
if(is_readable($data)) {
4849
//Read data from file
49-
$fileData = file($data, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
5050
$tmpData = [];
51-
foreach($fileData as $row) {
51+
$fileStream = fopen($data, "r");
52+
if(function_exists('ini_set')) {
53+
ini_set('auto_detect_line_endings', true);
54+
}
55+
while(($row = fgetcsv($fileStream, 0, $this->delimiter, $this->enclosure, $this->escape)) !== FALSE) {
5256
$tmpData[] = array_map(function($value) {
53-
$data = trim($value);
57+
$tmpValue = trim($value);
5458
//Remove enclosure
5559
if($this->ignoreEnclosure === true) {
56-
return $data;
60+
return $tmpValue;
5761
} else {
58-
return is_string($data) ? trim($data, $this->enclosure) : $data;
62+
return is_string($tmpValue) ? trim($tmpValue, $this->enclosure) : $tmpValue;
5963
}
60-
}, str_getcsv($row, $this->delimiter, $this->enclosure));
64+
}, $row);
6165
}
6266
$rawData = $tmpData;
6367
$dataType = 'csv';
@@ -148,6 +152,16 @@ public function setEnclosure(string $enclosure) {
148152
$this->enclosure = $enclosure;
149153
}
150154

155+
/**
156+
* Set csv escape
157+
*
158+
* @param string $escape
159+
* @return void
160+
*/
161+
public function setEscape(string $escape) {
162+
$this->escape = $escape;
163+
}
164+
151165
/**
152166
* Ignore csv header
153167
*

0 commit comments

Comments
 (0)