idct/php-csv-writer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

idct/php-csv-writer

Composer 安装命令:

composer require idct/php-csv-writer

包简介

CsvWriter based on a buffered TextWriter with EOL support.

README 文档

README

Basic CsvWriter based on a buffered TextWriter (also provided). Supports buffering in memory and proper handling of EOL setting which is not available easily with native fputcsv function.

Tests status Coverage Status GitHub tag (latest SemVer)

Main purpose of this library is to provide an object-oriented way of buffered CSV files writing.

Installation

The best way to install the library in your project is by using Composer:

composer require idct/php-csv-writer

of course you can still manually include all the required files in your project using using statements yet Composer and autoloading is more than suggested.

Usage

Create an instance:

use IDCT\CsvWriter\CsvWriter;
$csvWriter = new CsvWriter();

Now you need to open or create a file, to do so you have two methods available: open and openWithFieldsNames To create a new file with without adding headers in the first line use:

$csvWriter->open('filename.csv');

To open a new file with fields' names added in the first line pass an array as the second argument:

$csvWriter->openWithFieldsNames('filename.csv', ["columnA", "columnB", "columnC"]);

When writing a new line (adding data) to a CSV file opened with fields names then columns count number is verified on each attempt.

It is possible to appened into existing files by passing TextWriter::FILEMODE_APPEND as the last argument.

$csvWriter->open('filename.csv', CsvWriter::FILEMODE_APPEND);

or:

$csvWriter->openWithFieldsNames('filename.csv', ["columnA", "columnB", "columnC"], CsvWriter::FILEMODE_APPEND);

Warning: in append mode lines are NOT added as the first line of the CSV file, they are used only for verification of columns count during write operations.

Buffering

By default CsvWriter has buffering disabled, you can enable it by providing buffer size (in bytes) using method setBufferSize. Be sure to understand that data is actually saved in the file only when buffer is filled so be sure to close the file using close method or use flush whenever you want to intentionally flush the buffer into the file without closing it.

EOL support

By default PHP's internal fputcsv function uses default EOL symbol for the current platform. idct/php-csv-writer supports setting different EOL symbols: pass CsvWriter::EOL_WINDOWS, CsvWriter::EOL_LINUX or CsvWriter::EOL_MACLEGACY (Mac today uses LINUX EOL, this is just for legacy support) to setEolSymbol method. Warning: the EOL symbol will be used only for NEXT lines, previous ones (already existing in the file or buffer) will remain intact.

TextWriter

CsvWriter is based on TextWriter which can be used for buffered text write operations. Apart from the methods described above it provides also different behavior for write and writeln operations which store the text or write it followed by the defined EOL symbol respectively. To use it just create an instance:

$textWriter = new TextWriter();

TODO / Contribution

At the moment the main requirement is to provide better unit tests and documentation, yet if you find any bugs or have potential feature ideas then please use Issues or Pull Requests, it is more than welcome! I will try to reply ASAP.

统计信息

  • 总下载量: 2.02k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-11-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固