jlanger / csvreader
v1.1.0
2020-04-30 11:54 UTC
Requires
- php: >=7.1
Requires (Dev)
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-09-15 23:59:24 UTC
README
最小使用
use JLanger\CSV\CSV;
use JLanger\CSV\CsvConfig;
use JLanger\CSV\CsvFile;
use JLanger\CSV\Exceptions\CsvException;
require_once 'path-to-vendor-folder/vendor/autoload.php';
try {
$config = new CsvConfig();
// setzen der Headline:
$headlineArray = ['value1', 'value2', '...'];
$config->setHeadline($headlineArray);
$file = new CsvFile('path to file.csv');
$csv = new CSV($csvconfig);
$fileArr = $csv->read($file);
} catch (CsvException $e) {
trigger_error(get_class($e) . ': ' . $e->getMessage(), E_USER_NOTICE);
}
选项:$config->setDelimiter('delimiter') 设置分隔符,默认:,
$config->setEnclosure('enclosure') 设置封装符,默认:"
$config->setEscapeChar('char') 设置转义字符,默认:\
编写 csv 文件的示例
<?php
declare(strict_types=1);
use JLanger\CSV\CSV;
use JLanger\CSV\CsvConfig;
use JLanger\CSV\Exceptions\CsvException;
require_once __DIR__ . '/../vendor/autoload.php';
$config = new CsvConfig();
$config->setSafePath('')
->setFilename('test.csv');
$input = [
['h1', 'h2'],
[1, 2],
['l2', 'l3']
];
$csv = new CSV($config);
try {
$link = $csv->write($input);
} catch (CsvException $e) {
print_r($e->getMessage());
}
echo '<a href="' . $link['pathToFile'] . '">Link</a>';
所有错误都将抛出 CsvException 异常。