lukaswerner/php-csv
Composer 安装命令:
composer require lukaswerner/php-csv
包简介
A simple csv processing library for php
README 文档
README
This library focuses on simplicity and memory efficiency when processing CSV files in PHP.
Features
- Simple API
- Read CSV files line by line
- Memory efficiency: File is read line by line, no memory issues with large files
- Process CSV using row and chunk callbacks
- Fully documented
- Fully unit tested
- Composer ready
System Requirements
You need PHP >= 7.1.0 to use php-csv but the latest stable version of PHP is recommended.
Install
Install php-csv using Composer.
$ composer require lukaswerner/php-csv
Use
You can initialize an object of the main PhpCsv\Csv class by providing a valid \SplFileInfo object to the constructor:
$fileInfo = new \SplFileInfo('/path/to/file.csv');
$csv = new \PhpCsv\Csv($fileInfo);
You now have an easy to understand API to process your CSV file.
Each processing method will at first rewind the file, so that you don't have any issues with file pointers.
If your file does not contain a header row, you must call $csv->setContainsHeader(false);.
Number of lines
You can easily count the number of lines in your csv file by calling $csv->count();. This method
will only once examine all lines. On second call it will use its previously set value. To avoid that,
you have to create a new object of \PhpCsv\Csv.
Generator return values
The generator, created with $csv->rows() call will now have a return value if not aborted. That means,
after iterating through all lines in your CSV file, you can get the line count as
the generator return. Do something like this:
$rowsGenerator = $csv->rows();
foreach ($rowsGenerator as $row) {
// Do something
}
$numberOfLines = (int)$rowsGenerator->getReturn();
Please note, that this will throw an exception, if you use break in your loop. Just like that, the
$csv->process() will now return that generator return value or null, if aborted.
Reading header
You can read the header fields by calling $csv->header();. This will return an array of strings with header information.
This method will only once examine all lines. On second call it will use its previously set value. To avoid that,
you have to create a new object of \PhpCsv\Csv.
Iterating over rows
You can iterate over all rows of your csv file by doing something like this:
foreach ($csv->rows() as $lineNumber => $row) {
// Do somethine with row
}
The $lineNumber contains the current line number, starting with 1. The header (if existing) is not included.
So a file with a header and one line has $lineCount = 1 on that line.
The rows() method returns a PHP Generator and loops can be continued or stopped as you wish.
Processing using callbacks
The php-csv library offers a comfortable process() method to process the CSV file using callbacks. You can do something like this:
$csv->process(
function (array $row, int $lineNumber): bool {
// Do something with your data
return true; // If you return false here, the processing will stop
}
);
Chunking
The callback processing also allows chunking. That means, you have a callback for each row and additionally a callback for each chunk.
At first you can set the chunk size with $csv->setChunkSize(500) (default is 1000), then you call the process() method like this:
$csv->process(
function (array $row, int $lineNumber): bool {
// Do something with your data
return true; // If you return false here, the processing will stop
},
function (): void {
// Will be called e.g. every 500th row
// you can flush your values to db or something like that
}
);
Please note, that the chunk callback will be called one last time, when you return false from the row callback.
That is to be able to flush incomplete chunks.
Contributing
Contributions are welcome and will be fully credited. Please feel free to open issues and ask for repository access.
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The GNU General Public License v3.0. Please see LICENSE for more information.
lukaswerner/php-csv 适用场景与选型建议
lukaswerner/php-csv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.09k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「csv」 「import」 「processing」 「read」 「write」 「chunk」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lukaswerner/php-csv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lukaswerner/php-csv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lukaswerner/php-csv 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
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.
paywant api php client
A fork of konnco/filament-import with support of Laravel 11 since the default importer of Filament 3 is nonsense for basic use case.
Convert and operate with FIPS codes for states, counties, etc.
laravel facade to read/write csv file
统计信息
- 总下载量: 25.09k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2019-01-13