rskuipers / csv
1.1.2
2014-07-08 13:54 UTC
Requires
- php: >=5.3.3
- ext-intl: *
Requires (Dev)
- pdepend/pdepend: 1.*
- phing/phing: 2.*
- phploc/phploc: 2.*
- phpmd/phpmd: 1.*
- phpunit/phpunit: 4.*
- sebastian/phpcpd: 2.*
- squizlabs/php_codesniffer: 1.*
This package is auto-updated.
Last update: 2023-07-14 20:44:45 UTC
README
这是什么
这是另一个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