定制 gugglegum/csv-rw 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

gugglegum/csv-rw

Composer 安装命令:

composer require gugglegum/csv-rw

包简介

CSV reader and writer

README 文档

README

This is easy to use Composer package to read and write CSV files in very convenient way. The CSV Reader is actually an OO-wrapper on the fgetcsv() built-in PHP function. It adds exceptions on errors and implements Iterator interface. You can iterate CSV reader in foreach statement like array. It supports headers of columns, so you may handle with associative data arrays. Headers may be read from first line of CSV file or be set manually. Or be read from first line and then changed by some reflection function or whatever you want.

Instead of opening you also can assign already opened file handle or stream. This package contains validation of data consistency. For example, it throws an exception if some row contains less or more columns than previous one. Therefore you don't need to always check type of return values. Just use try .. catch statement and catch exceptions if necessary. The package uses strong typing for scalars came to us from PHP7.

Usage

See more examples of usage in /test folder.

Read CSV

use \gugglegum\CsvRw\CsvReader;
use \gugglegum\CsvRw\CsvFormat;

$csv = new CsvReader(new CsvFormat([
    'delimiter' => ',',
    'enclosure' => '"',
    'escape' => '\\',
]));
$csv->open('input.csv', CsvReader::WITH_HEADERS);

foreach ($csv as $row) {
    var_dump($row);
}

$csv->close();

Write CSV

use gugglegum\CsvRw\CsvFormat;
use gugglegum\CsvRw\CsvWriter;

$headers = ['id', 'firstName', 'lastName'];

$rows = [
    [
        'id' => 1,
        'firstName' => 'John',
        'lastName' => 'Smith',
    ],
];

$csv = new CsvWriter(new CsvFormat([
   'delimiter' => ',',
   'enclosure' => '"',
   'escape' => '\\',
]));

$csv->open('output.csv', CsvWriter::WITH_HEADERS, $headers);

foreach ($rows as $row) {
    $csv->writeRow($row);
}

$csv->close();

Installation

This library is available as composer package. To start using composer in your project follow these step

Install Composer

curl -s http://getcomposer.org/installer | php
mv ./composer.phar ~/bin/composer # or /usr/local/bin/composer

Install this package

composer require gugglegum/csv-rw

Add composer's autoloader to you code

require 'vendor/autoload.php';

Now you can use CSV reader & writer in your code.

Troubleshooting

If you have troubles with MAC's line-endings \r, you may turn on PHP option auto_detect_line_endings:

When turned on, PHP will examine the data read by fgets() and file() to see if it is using Unix, MS-Dos or Macintosh line-ending conventions.

It may be turned on from you PHP code. Just add this before reading MAC files:

ini_set('auto_detect_line_endings', true);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-11-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固