本文整理汇总了PHP中SplFileObject::getCsvControl方法的典型用法代码示例。如果您正苦于以下问题:PHP SplFileObject::getCsvControl方法的具体用法?PHP SplFileObject::getCsvControl怎么用?PHP SplFileObject::getCsvControl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplFileObject
的用法示例。
在下文中一共展示了SplFileObject::getCsvControl方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getEnclosure
/**
* @return string
*/
public function getEnclosure()
{
if ($this->enclosure === null) {
list($delimiter, $this->enclosure) = $this->csvFile->getCsvControl();
}
return $this->enclosure;
}
示例2: SplFileObject
<?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;
}
示例3: SplFileObject
<?php
$obj = new SplFileObject(dirname(__FILE__) . '/SplFileObject_testinput.csv');
var_dump($obj->getCsvControl());
示例4: readFile
/**
* 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];
}