vansari/php-csv
Composer 安装命令:
composer require vansari/php-csv
包简介
Simple CSV Reader/Writer library
README 文档
README
Simple CSV Reader/Writer as a learning project.
Set the options with an Strategy for Read and Write
- Basic features: Read CSV with different Delimiters, Enclosures, Escapes
- Extra Options: ** set the Encoding for read/write csv. (Read file with given encoding, Write file with given encoding) ** skip a number of leading lines (read only) ** do you want to skip empty Records? (read only) ** has the csv a header?
Functionality of the Reader
- Set that the Reader should normalize the header (Replacing unknown Chars or replace WS with underscore etc)
- read only the Header
- count the Fields
- read one Record
- read all Records
- read a Record at a Line Index (zerobased without header)
- read Records from a Range n to n (zerobased without header)
Usage of the Reader:
// Reader must be construct with the existing file and it creates an Default Strategy in the constructor $reader = new Reader($filepath); /* * Default Strategy is * Delimiter = ',' * Enclosure = '"' * Escape = "\\" * Encoding = "UTF-8" * hasHeader = true * skipEmptyLines = true * skipLeadingLines = 0 * asAssociative = false (each records as ['headerfield' => value, ...]) */ // now manipulate the strategy how to read your csv $reader->getStrategy() ->setDelimiter("\t") ->setEncoding(CharsetEncoding::ISO_8859_1) // the encoding of the file ->setHasHeader(false) // if csv doesn't contain header ->setSkipEmptyRecords(true); // you have also the possibility to instances a new Strategy object $strategy = Strategy::create(); // with default values // Than you can set the Strategy $reader->setStrategy($strategy); // if you want to read one Record $record = $reader->readRecord(); // will return the first record $nextRecord = $reader->readRecord(); // will return the next record // you can also use an while loop while (null !== ($record = $reader->readRecord())) { // ... do your stuff }
The Writer
The Writer uses the same Strategy, Encoding etc as the Reader. Also the usage is very simple.
Options for Write take a look at the top of this File
...
Functionality of the Writer
- instantiate the writer with targetPath (optional), filename (optional) and if you want to append (default false) the content to the file
- Writer instantiates the standard Strategy in the constructor
- set the Header with an array of strings which are not empty (null or '')
- you can normalize the Header with the HeaderNormalizer
- write only one Record (Write the record to temporary file)
- write an array of Records (Write the records to temporary file)
- and write Records to csv File
- get Tempfilepath for moving the temporary file to the expected csv
Usage of the Writer
// First instanciate a standard writer without targetpath, filename and not appending records $writer = new Writer(); // Change the Strategy $writer->getStrategy() ->setDelimiter("\t") ->setEncoding(CharsetEncoding::ISO_8859_1) // the encoding of the file ->setSkipEmptyRecords(true); $header = [ 'column1', 'column2', 'column3', ]; $writer->writeHeader($header); // will return true on sucess // Than write all Records (multidimensional array which contains each row as separate array) $records = [ [ 'foo' => 1, 'bar' => 'bar', 'baz' => 1.56, ], [ 'foo' => 'Foo Bar Man', 'bar' => 34.98, 'baz' => 1000, ], ]; $writer->writeRecords($records); // will return true if successful otherwise false /* content of temp file are tab separated records with header: column1 column2 column3 1 bar 1.56 "Foo Bar Man" 34.98 1000 */
Benefit
use the Reader and Writer as a composition
$reader = new Reader($file); $reader->getStrategy() ->setDelimiter("\t") ->setEncoding(CharsetEncoding::ISO_8859_1) // the encoding of the file ->setSkipEmptyRecords(true); $writer = new Writer($targetPath, 'file_without_empty_lines.csv'); $writer->setStrategy($reader->getStrategy()); $writer->writeHeader($reader->getHeader()); $writer->writeRecords($reader->readAllRecords());
What is planned?
I will implement an Transformer which should transform the Values from string to php int, float etc and vis versa. It should also normalize the float values with different decimal character.
If you have any questions or ideas don't hesitate to contact me. Your are free to copy, fork or support this little project ;-)
vansari/php-csv 适用场景与选型建议
vansari/php-csv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 11 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 vansari/php-csv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vansari/php-csv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2019-11-27