2017 © Pedro Peláez
 

library csv

CSV Reader and Writer

image

phillipsdata/csv

CSV Reader and Writer

  • Friday, March 4, 2016
  • by clphillips
  • Repository
  • 3 Watchers
  • 0 Stars
  • 283 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 5 Versions
  • 9 % Grown

The README.md

Csv

Build Status Coverage Status, (*1)

A CSV reader and writer with no external dependencies, that allows just in time formatting and filtering, and operates on files as well as streams., (*2)

Install

composer require phillipsdata/csv

Basic Usage

Reading a CSV

<?php

use PhillipsData\Csv\Reader;

// Set the input for the reader
$reader = Reader::input(new SplFileObject('php://stdin'));

$lines = [];
// Fetch the result for each line read
foreach ($reader->fetch() as $line) {
    $lines[] = $line;
}

Writing a CSV

<?php

use PhillipsData\Csv\Writer;

$writer = Writer::output(new SplFileObject('/path/to/file.csv'));

$data = [
    ['colA', 'colB'],
    ['A1', 'B1'],
    ['A2', 'B2']
];

// Write all rows (works great with Iterators)
$writer->write($data);

// Or, write a single row at a time
foreach ($data as $row) {
    $writer->writeRow($row);
}

Factories

<?php

use PhillipsData\Csv\Factory;

// returns \PhillipsData\Csv\Writer
$writer = Factory::writer('path/to/file', ',', '"', '\\');

// returns \PhillipsData\Csv\Reader
$reader = Factory::reader('path/to/file', ',', '"', '\\');

Formatting and Filtering

Formatting and Filtering work for both reading and writing., (*3)

To format data while iterating, simply specify the format callback function., (*4)


// The formatter is called for each line parsed $reader->format(function ($line, $key, $iterator) { return [ 'first_name' => $line['First Name'], 'last_name' => $line['Last Name'], 'email' => strtolower($line['Email']), 'date' => date('c', strtotime($line['Date'])) ]; }); foreach ($reader->fetch() as $line) { // $line now contains 'first_name', 'last_name', 'email', and 'date'. }

To filter data while iterating, simply specify the filter callback function., (*5)


// The formatter is called for each line parsed $reader->filter(function ($line, $key, $iterator) { return $line['Last Name'] === 'Smith'; }); foreach ($reader->fetch() as $line) { // $line only contains records where $line['Last Name'] === 'Smith' }

The Versions

04/03 2016

dev-master

9999999-dev

CSV Reader and Writer

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

csv file parser delimited tab

04/03 2016

1.1.0

1.1.0.0

CSV Reader and Writer

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

csv file parser delimited tab

04/03 2016

dev-factory-set-file-openmode

dev-factory-set-file-openmode

CSV Reader and Writer

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

csv file parser delimited tab

23/02 2016

1.0.0

1.0.0.0

CSV Reader and Writer

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

csv file parser delimited tab

16/02 2016

dev-reader

dev-reader

CSV Reader and Writer

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

csv file parser delimited tab