此包已被弃用,不再维护。作者建议使用 league/csv 包。

简单的CSV库

1.1.2 2014-07-08 13:54 UTC

This package is auto-updated.

Last update: 2023-07-14 20:44:45 UTC


README

Build Status

这是什么

这是另一个CSV库,旨在帮助您轻松开始读取CSV文件。此库允许您设置一行作为列标题,并使用列标题访问值。您还可以设置列格式化程序来解析(本地化)货币、小数或创建自己的列格式化程序。

安装

将以下行添加到您的composer require

"rskuipers/csv": "~1.0.0"

示例

映射模式

ID,Name,Age
1,John,19
2,Doe,21
3,Foo,31
4,Bar,52
use RSKuipers\CSV\File;

$file = new File($this->getCSVFile(), 0);
$file->setMappingMode(File::COLUMN_TITLES);
$row = $file->fetch();
echo $row['Name'];
use RSKuipers\CSV\File;

$file = new File($this->getCSVFile(), 0);
$file->setMappingMode(File::INDEX);
$row = $file->fetch();
echo $row[1];
use RSKuipers\CSV\File;

$file = new File($this->getCSVFile(), 0);
$file->setMappingMode(File::CUSTOM);
$file->setMapping(array(
    'ID',
    'Product Name',
    'Price',
));
$row = $file->fetch();
echo $row['Product Name'];

列格式化程序

ID,Name,Price,Stock
1,Lighter,"€ 15,95","2.093.230"
2,Chair,"€ 17","3.230"
3,Table,"€ 19,91","530",
4,Book,"€ 1","76.126"
use RSKuipers\CSV\File;
use RSKuipers\CSV\Formatter\Currency as CurrencyFormatter;
use RSKuipers\CSV\Formatter\Decimal as DecimalFormatter;

$priceFormatter = new CurrencyFormatter('nl_NL');
$decimalFormatter = new DecimalFormatter('nl_NL');
$csv = new File($this->getCSVFile(), 0);
$csv->setMappingMode(File::COLUMN_TITLES);
$csv->setFormatter('Price', $priceFormatter);
$csv->setFormatter('Stock', $decimalFormatter);
$row = $csv->fetch();
echo $row['Price']; // 15.95
echo $row['Stock']; // 2093230

测试

运行Phing以执行PHPLint、PHPCS、PHPMD和PHPUnit。确保您使用了composer install/update来安装开发依赖项。

$ ./vendor/bin/phing