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 (44)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-11-13 (3 hours ago) RSS 2.0 feedStarStarStarStar 70%Total: 353 This week: 18All time: 6,765 This week: 20Up
Version License PHP version Categories
php-component-csv 1.5.18GNU 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
  Performance   Level  
Name: nvb <contact>
Classes: 5 packages by
Country: Germany Germany
Innovation award
Innovation award
Nominee: 2x

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 because of missing compatibility with php 5.6 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.

The current change log can be found here.

Benefits

  • low and stable memory usage (give it a try by using benchmarkReader and benchmarkWriter)
  • works with PHP 5.6 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');
//I am using json_encode() since there is no official and best way how to
// output arrays on the command line.

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

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

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

By Iteration

$reader = new Reader('my/file.csv');
//I am using json_encode() since there is no official and best way how to
// output arrays on the command line.

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

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

By Using As A Function

$reader = new Reader('my/file.csv');
//I am using json_encode() since there is no official and best way how to
// output arrays on the command line.

while ($line = $reader()) {
    echo json_encode($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($line);
}

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 "file.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.

Other Great Components

Hall of Fame - The list of contributors

Contributing

Please see CONTRIBUTING for details.

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 CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md 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:353
This week:18
All time:6,765
This week:20Up
 User Ratings  
 
 All time
Utility:91%StarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:66%StarStarStarStar
Examples:-
Tests:91%StarStarStarStarStar
Videos:-
Overall:70%StarStarStarStar
Rank:349