|
15 | 15 | class CSV { |
16 | 16 | private $delimiter = ','; |
17 | 17 | private $enclosure = '"'; |
| 18 | + private $escape = '\\'; |
18 | 19 | private $ignoreHeader = false; |
19 | 20 | private $ignoreHeaderCase = true; |
20 | 21 | private $ignoreEnclosure = false; |
@@ -46,18 +47,21 @@ public function parse($data) { |
46 | 47 | if(is_file($data)) { |
47 | 48 | if(is_readable($data)) { |
48 | 49 | //Read data from file |
49 | | - $fileData = file($data, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
50 | 50 | $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) { |
52 | 56 | $tmpData[] = array_map(function($value) { |
53 | | - $data = trim($value); |
| 57 | + $tmpValue = trim($value); |
54 | 58 | //Remove enclosure |
55 | 59 | if($this->ignoreEnclosure === true) { |
56 | | - return $data; |
| 60 | + return $tmpValue; |
57 | 61 | } else { |
58 | | - return is_string($data) ? trim($data, $this->enclosure) : $data; |
| 62 | + return is_string($tmpValue) ? trim($tmpValue, $this->enclosure) : $tmpValue; |
59 | 63 | } |
60 | | - }, str_getcsv($row, $this->delimiter, $this->enclosure)); |
| 64 | + }, $row); |
61 | 65 | } |
62 | 66 | $rawData = $tmpData; |
63 | 67 | $dataType = 'csv'; |
@@ -148,6 +152,16 @@ public function setEnclosure(string $enclosure) { |
148 | 152 | $this->enclosure = $enclosure; |
149 | 153 | } |
150 | 154 |
|
| 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 | + |
151 | 165 | /** |
152 | 166 | * Ignore csv header |
153 | 167 | * |
|
0 commit comments