jeroenzwart/php-csv-iterator
Composer 安装命令:
composer require jeroenzwart/php-csv-iterator
包简介
A simple CSV reader, to read as array/object with PHP Iterator
README 文档
README
A simple CSV reader with PHP Iterator
This package is an easy way to read CSV files. The CSV Reader will iterate over a CSV file with low memory usage.
Features
- Returns an array with keys from the headers.
- Reads a specific line in the CSV file.
- Skip the empty lines.
- Reading a CSV file with an offset and/or limit.
Installation
Via Composer
$ composer require jeroenzwart/php-csv-iterator
Usage
This example is a part of the CSV file ./csv/movies.csv;
name,year_release,order,imdb_rating
Star Wars Episode I – The Phantom Menace,1999,1,6.5
Star Wars Episode II – Attack of the Clones,2002,2,6.5
To loop over each item in the CSV file, you will use the reader like this;
$csv = new \JeroenZwart\CsvIterator\CsvReader('../csv/movies.csv'); $csv->delimiter('"') foreach ($csv as $line) { var_dump($line) // Or do something with $line... } // The first dump will look like this // class stdClass(4) { // public 'name' => string(42) "Star Wars Episode I – The Phantom Menace" // public 'year_release' => string(4) "1999" // public 'order' => string(1) "1" // public 'imdb_rating' => string(3) "6.5" // }
Or use one of the default iterator methods;
$csv = new \JeroenZwart\CsvIterator\CsvReader('../csv/movies.csv'); $line = $csv->next()->current();
Options
filePath(string) - Path to the CSV file.offset(integer 0) - The offset from start reading the CSV file.limit(integer -1) - The limit to end reading the CSV file.delimiter(string ,) - The delimiter character in the CSV file.enclosure(string ") - The enclosure character in the CSV file.escape(string \) - The escape character in the CSV file.hasHeaders(boolean TRUE) - To set if the CSV file has a header, set FALSE if not.asObject(boolean TRUE) - Return the lines of the CSV file as object, set FALSE as array.keepEmptyLines(boolean FALSE) - Set TRUE for keeping an empty lines in the CSV file.
Examples
Offset and limit
Start reading with another position with offset and limit;
$csv = new CsvReader('../csv/actors.csv', 3, 5); foreach ($csv as $line) { // Do something with $line... }
Delimiter, enclose and escape
Read with another delimiter, enclose and escape;
$csv = new CsvReader('../csv/actors.csv'); $csv->delimiter(';')->enclose('`')->escape(''); foreach ($csv as $line) { // Do something with $line... }
To get the current delimiter, enclosure or escape, you use $csv->delimiter()
Headers, asObject and empty
Ignore the headers in the CSV file and return array with regular keys, but keep empty lines;
$csv = new CsvReader('../csv/actors.csv'); $csv->headers(false)->asObject(false)->empty(true); foreach ($csv as $line) { // Do something with $line... }
To get the headers of a CSV file as array, you use $csv->headers().
For getting the modes for settings asObject or empty, you can use $csv->asObject() or $csv->empty().
Position
Load a line at a given position;
$csv = new CsvReader('../csv/actors.csv'); var_dump($csv->position(3));
To get the current position of the iterator, you use $csv->position()
Change log
Please see the changelog for more information on what has changed recently.
License
Please see the license file for more information.
jeroenzwart/php-csv-iterator 适用场景与选型建议
jeroenzwart/php-csv-iterator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.09k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 04 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「iterator」 「csv」 「import」 「reader」 「open」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jeroenzwart/php-csv-iterator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jeroenzwart/php-csv-iterator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jeroenzwart/php-csv-iterator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
collection php library
PHP PDO Mysql select statement iterator implemented as multiple queries using LIMIT clauses
Iteration tools for PHP
Sequential way to run Symfony Processes in parallel
Extends XMLReader PHP class, for simple SAX-reading and XPath queries of huge XML files.
Trait providing methods implementing the Iterator interface.
统计信息
- 总下载量: 1.09k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2020-04-28