PHP Classes
elePHPant
Icontem

CSV Component for PHP: Reader and writer for CSV files

Recommend this page to a friend!
  Info   View files Documentation   View files View files (42)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2016-05-31 (3 months ago) RSS 2.0 feedNot enough user ratingsTotal: 165 All time: 8,056 This week: 867Up
Version License PHP version Categories
php-component-csv 1.5.12GNU Lesser Genera...5.3PHP 5, Files and Folders
Description Author

This package implements a reader and writer for CSV files.

It implements a reader interface that can perform several types of operations to read CSV files, like reading the headline record, reading one record, reading multiple records or reading all records.

The writer interface can write the headline record, write one record and write multiple records.

The package can also truncate a CSV file to create a file with no records, copy a CSV file to another, or move a CSV file to another file name.

Recommendations
Picture of nvb
  Performance   Level  
Name: nvb <contact>
Classes: 5 packages by
Country: Germany Germany
Age: ???
All time rank: 2808172 in Germany Germany
Week rank: 15 Up2 in Germany Germany Up
Innovation award
Innovation award
Nominee: 1x

Details

CSV Handling Component for PHP

This project aims to deliver an easy to use and free as in freedom php compontent for dealing with csv files (read and write).

This component is heavily influenced by jwage/easy-csv. It was mainly created created because of missing compatibility with php 5.3 and no official packagist support from jwage/easy-csv.

The build status of the current master branch is tracked by Travis CI: Build Status Latest stable

The scrutinizer status are: code quality

The versioneye status is: Dependency Status

Take a look on openhub.net.

Benefits

  • low and stable memory usage (give it a try by using benchmarkReader and benchmarkWriter)
  • works with PHP 5.3 and above
  • \_\_invoke() implemented to use it as function
  • unified reader and writer
  • adapter to easy up migration from EasyCsv - 0.0.1 to this component * writer * reader
  • usage of filters - control what comes in and what comes out
  • reader * implemented iterator * readOne(); * readMany(); * readAll();
  • writer * copy(); * delete(); * move(); * truncate(); * writeOne(); * writeMany(); * writeAll(); //truncates file and writes content

Install

By Hand

mkdir -p vendor/net_bazzline/php_component_csv
cd vendor/net_bazzline/php_component_csv
git clone https://github.com/bazzline/php_component_csv .

With Packagist

    composer require net_bazzline/php_component_csv:dev-master

Usage

Reader

Read Content

$reader = new Reader('my/file.csv');

//read one line
echo $reader->readOne() . PHP_EOL;

//read 10 lines
foreach ($reader->readMany(10) as $line) {
    echo $line . PHP_EOL;
}

//read all lines
foreach ($reader->readAll() as $line) {
    echo $line . PHP_EOL;
}

By Iteration

$reader = new Reader('my/file.csv');

if ($reader->hasHeadline()) {
    echo 'headlines: ' . $reader->readHeadline();
}

foreach ($reader as $line) {
    echo $line . PHP_EOL;
}

By Using As A Function

$reader = new Reader('my/file.csv');

while ($line = $reader()) {
    echo $line . PHP_EOL;
}

Writer

Write Content

By Iteration

//$headlines contains a php array
//$lines contains a php array of arrays
$writer = new Writer('my/file.csv');

$writer->writeHeadline($headlines);

foreach ($lines as $line) {
    $writer->writeOne($line);
}

At Once

//$headlines contains a php array
//$lines contains a php array of arrays
$writer = new Writer('my/file.csv');

$writer->writeHeadline($headlines);
$writer->writeMany($lines);

By Using As A Function

//$line contains a php array
//$lines contains a php array of arrays
$writer = new Writer('my/file.csv');

$writer($line);

foreach ($lines as $line) {
    $writer($lines);
}

Truncate

$writer = new Writer('my/file.csv');

$writer->truncate();

Copy

$writer = new Writer('my/file.csv');

$writer->copy('my/my_first_copy.csv');    //writer will still write into "my_first_copy.csv"

$writer->copy('my/my_second_copy.csv', true);    //writer will write in "my_second_copy.csv"

Move

$writer = new Writer('my/file.csv');

$writer->move('my/new_name.csv');   //writer will write in "new_name.csv"

API

API is available at bazzline.net.

History

  • upcomming * @todo * add example for filter usage * add documentation for filter usage * extend unit tests * implement \_\_clone(); * implement usage of filter in writer::copy * write documentation * write adapter to easy up migration from EasyCsv - 0.0.2 to this component
  • 1.5.14 - released at 30.05.2016 * relaxed dependency for mockery
  • 1.5.13 - released at 16.03.2016 * updated dependency
  • 1.5.12 - released at 22.02.2016 * moved to psr-4 autoloading * removed build status from scrutinizer section * updated depencenies
  • 1.5.11 - released at 20.01.2016 * updated depencenies
  • 1.5.10 - released at 12.01.2016 fixed dependency handling for phpunit 4.8.
  • 1.5.9 - released at 11.12.2015 * updated dependencies
  • 1.5.8 - released at 08.11.2015 * updated dependencies
  • 1.5.7 - released at 07.10.2015 * updated dependencies
  • 1.5.6 - released at 10.09.2015 * relaced dependencies
  • 1.5.5 - released at 10.09.2015 * relaced dependencies
  • 1.5.4 - released at 09.09.2015 * added BaseInterface, ReaderInterface and WriterInterface
  • 1.5.3 - released at 26.08.2015 * updated dependencies
  • 1.5.2 - released at 06.07.2015 * refactored cli example by using php_component_cli_readline
  • 1.5.1 - released at 04.07.2015 * updated dependency
  • 1.5.0 - released at 02.07.2015 * added dependency to generic agreement * replaced own FilterInterface with external FilterInterface
  • 1.4.0 - released at 02.07.2015 * started cli example to easy up usage * added "rewind" call when using reader::readAll() and reader::readMany()
  • 1.3.0 - released at 26.06.2015 * added headline output support as keys for Reader::readMany() * added headline output support as keys for Reader::readOne() * can be disabled by Reader::disableAddHeadlineToOutput() * can be enabled by Reader::enableAddHeadlineToOutput() * is enabled by default * fixed broken unit test for php 5.3 * moved complex array combine into own project * removed duplicated code in Reader
  • 1.2.0 - released at 25.06.2015 * added examples (benchmarkReader, benchmarkWriter, reader and writer) * implemented filter for reader and writer by creating the FilterInterface
  • 1.1.0 - released at 10.06.2015 * added link to api * added minimum php version requirement * implemented "move($path)" method into Writer * removed "TODO" * updated dependencies
  • 1.0.0 - released at 07.06.2015 * initial release

Other Great Components

Final Words

Star it if you like it :-). Add issues if you need it. Pull patches if you enjoy it. Write a blog entry if you use it. Donate something if you love it :-].

  Files folder image Files  
File Role Description
Files folder imageexample (5 files)
Files folder imagesource (6 files, 3 directories)
Files folder imagetest (8 files)
Accessible without login Plain text file .gitignore Data Auxiliary data
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Data Auxiliary data
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  example  
File Role Description
  Accessible without login Plain text file benchmarkReader Data Auxiliary data
  Accessible without login Plain text file benchmarkWriter Data Auxiliary data
  Accessible without login Plain text file cli Data Auxiliary data
  Accessible without login Plain text file reader Data Auxiliary data
  Accessible without login Plain text file writer Data Auxiliary data

  Files folder image Files  /  source  
File Role Description
Files folder imageFilter (2 files)
Files folder imageReader (6 files)
Files folder imageWriter (8 files)
  Plain text file AbstractBase.php Class Class source
  Plain text file AbstractFactory.php Class Class source
  Plain text file BaseInterface.php Class Class source
  Plain text file FactoryInterface.php Class Class source
  Plain text file InvalidArgumentException.php Class Class source
  Plain text file RuntimeException.php Class Class source

  Files folder image Files  /  source  /  Filter  
File Role Description
  Plain text file AbstractFilter.php Class Class source
  Plain text file PermeableFilter.php Class Class source

  Files folder image Files  /  source  /  Reader  
File Role Description
  Plain text file EasyCsvReaderAdapter.php Class Class source
  Plain text file FilteredReader.php Class Class source
  Plain text file FilteredReaderFactory.php Class Class source
  Plain text file Reader.php Class Class source
  Plain text file ReaderFactory.php Class Class source
  Plain text file ReaderInterface.php Class Class source

  Files folder image Files  /  source  /  Writer  
File Role Description
  Plain text file EasyCsvWriterAdapter.php Class Class source
  Plain text file FilteredWriter.php Class Class source
  Plain text file FilteredWriterFactory.php Class Class source
  Plain text file FilteredWriterForPhp3Dot3.php Class Class source
  Plain text file Writer.php Class Class source
  Plain text file WriterFactory.php Class Class source
  Plain text file WriterForPhp5Dot3.php Class Class source
  Plain text file WriterInterface.php Class Class source

  Files folder image Files  /  test  
File Role Description
  Accessible without login Plain text file AbstractTestCase.php Test Unit test script
  Accessible without login Plain text file bootstrap.php Test Unit test script
  Accessible without login Plain text file EasyCsvReaderAdapterTest.php Test Unit test script
  Accessible without login Plain text file EasyCsvWriterAdapterTest.php Test Unit test script
  Accessible without login Plain text file FilteredReaderTest.php Test Unit test script
  Accessible without login Plain text file FilteredWriterTest.php Test Unit test script
  Accessible without login Plain text file ReaderTest.php Test Unit test script
  Accessible without login Plain text file WriterTest.php Test Unit test script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:165
This week:0
All time:8,056
This week:867Up