/** * @return string */ public function getEnclosure() { if ($this->enclosure === null) { list($delimiter, $this->enclosure) = $this->csvFile->getCsvControl(); } return $this->enclosure; }
<?php $file = new SplFileObject(__DIR__ . '/csv.csv'); $file->setFlags(SplFileObject::READ_CSV); $file->setCsvControl(';'); var_dump($file->getCsvControl()); foreach ($file as $line) { var_dump($line); exit; }
<?php $obj = new SplFileObject(dirname(__FILE__) . '/SplFileObject_testinput.csv'); var_dump($obj->getCsvControl());
/** * read file using phpexcel library * * @param string $source * @return array */ private function readFile($source, $limit = null) { //load the phpexcel IOFactory class include $this->_docRoot . '/vendor/phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php'; //prepare the reader, try { $type = PHPExcel_IOFactory::identify($source); if (stripos($type, 'excel') === false && stripos($type, 'csv') === false) { $error = ['error' => 'This is not a spreadsheet file.']; return $error; } $reader = PHPExcel_IOFactory::createReader($type); //if type is csv (.csv or .txt), find the delimeter if ($type == 'CSV') { $csvChecker = new SplFileObject($source); $csvProperty = $csvChecker->getCsvControl(); $delimiter = $csvProperty[0]; $reader->setDelimiter($delimiter); } $object = $reader->load($source); } catch (Exception $e) { $error = ['error' => 'I cannot read the file.']; return $error; } //extract the data from the first sheet of the file try { $sheet = $object->getSheet(0); $rows = $sheet->getHighestRow(); $columns = $sheet->getHighestColumn(); } catch (Exception $e) { $error = ['error' => 'I cannot process the first sheet.']; return $error; } //there should be at least 1 row of data if ($rows < 1) { $error = ['error' => 'There is not enough data to read.']; return $error; } //read just first 10 rows if it is not for full processing $last = $rows; $max = !$limit || $rows < $limit + 1 ? $rows : $limit; //read the data, metadata into keys and data into data $data = []; try { for ($row = 1; $row <= $max; $row++) { $data[] = $sheet->rangeToArray('A' . $row . ':' . $columns . $row, NULL, TRUE, FALSE); } //add last row if not full processing if ($limit && $rows > 10) { $data[] = $sheet->rangeToArray('A' . $last . ':' . $columns . $last, NULL, TRUE, FALSE); } } catch (Exception $e) { $error = ['error' => 'I cannot read the data.']; return $error; } if (!is_numeric($columns)) { $range = range('A', 'Z'); $columns = array_search($columns, $range) + 1; } return ['data' => $data, 'columns' => $columns]; }