palmtree/csv
Composer 安装命令:
composer require palmtree/csv
包简介
CSV Reader and Writer
README 文档
README
A CSV reader and writer for PHP.
The Reader class implements the Iterator interface, loading one line into memory at a time. This means large files can be parsed
without hitting any memory limits.
Requirements
- PHP >= 7.4
Installation
Use composer to add the package to your dependencies:
composer require palmtree/csv
Usage
Reading
Reading from a CSV File
$csv = new Reader('people.csv'); foreach($csv as $row) { $name = $row['name']; if (isset($row['age'])) { echo "age is set!"; } }
Normalize Data Types
A number of different normalizers can be used to convert data from strings into certain data types. Below is contrived example using some of the currently bundled normalizers:
$csv = new Reader('/path/to/products.csv'); $csv->addNormalizers([ // Convert to integer 'product_id' => new NumberNormalizer(), // Keep data as string but trim it 'name' => new StringNormalizer(), // Convert to float, rounded to 4 decimal places 'price' => NumberNormalizer::create()->scale(4), // Convert to boolean true or false 'enabled' => new BooleanNormalizer(), // Convert to an array of integers 'related_product_ids' => new ArrayNormalizer(new NumberNormalizer()), // Custom conversion with a callback 'specials' => new CallableNormalizer(fn ($value) => json_decode($value)), ]);
No Headers
If your CSV contains no headers pass false as the second argument to the constructor:
$csv = new Reader('people.csv', false); // Alternatively, call the setHasHeaders() method after instantiation: //$csv->setHasHeaders(false);
Header Offset
If your CSV headers are not on the first row you may specify the (zero based) row offset:
$csv = new Reader('people.csv'); // Headers are on the second row so let's set the offset to 1 $csv->setHeaderOffset(1);
Inline Reading
You may use the InlineReader to parse a CSV string rather than a file, if it was obtained from an API call or some other means:
$csv = new \Palmtree\Csv\InlineReader('"header_1","header_2"' . "\r\n" . '"foo","bar"');
Writing
Build and Download a CSV File
$people = []; $people[] = [ 'name' => 'Alice', 'age' => '24', 'gender' => 'Female', ]; $people[] = [ 'name' => 'Bob', 'age' => '28', 'gender' => 'Male', ]; Downloader::download('filename.csv', $people);
Writing to a CSV File
$people = []; $people[] = [ 'name' => 'Alice', 'age' => '24', 'gender' => 'Female', ]; $people[] = [ 'name' => 'Bob', 'age' => '28', 'gender' => 'Male', ]; Writer::write('/path/to/output.csv', $people);
See the examples directory for more usage examples.
Advanced Usage
CSV Control
You can access the document object to change the CSV delimiter, enclosure and escape character:
$csv = new Reader('/path/to/input.csv'); $csv->setDelimiter("\t"); $csv->setEnclosure('"'); $csv->setEscapeCharacter("\\");
Line Endings
CSVs default to \r\n line endings. Access the document object if you need to change this:
$csv = new Writer('/path/to/output.csv'); $csv->getDocument()->setLineEnding("\n");
Fine-grained Control
The document object extends PHP's SplFileObject and inherits its methods:
$csv = new Reader('/path/to/input.csv'); $csv->getDocument()->setFlags(\SplFileObject::DROP_NEW_LINE);
Configuration
If you're trying to read a CSV file in or generated by an old Mac computer you may need to include
the following snippet before creating a new Reader instance:
if (!ini_get('auto_detect_line_endings')) { ini_set('auto_detect_line_endings', '1'); }
This is because Macs used to use \r as a line separator. See here for more details.
License
Released under the MIT license
palmtree/csv 适用场景与选型建议
palmtree/csv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.48k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 10 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「csv」 「import」 「export」 「read」 「write」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 palmtree/csv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 palmtree/csv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 palmtree/csv 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bulk export of sylius resources
Parse use statements for a reflection object
Tool for copying data from a production database to a dev database. Also useful for making backups of production databases.
A fork of konnco/filament-import with support of Laravel 11 since the default importer of Filament 3 is nonsense for basic use case.
Yii2 export extension
laravel facade to read/write csv file
统计信息
- 总下载量: 6.48k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-10-25