tleckie / csv
PHP csv reader and writer
Fund package maintenance!
teodoroleckie
www.paypal.com/donate?business=ZHYA2MTGA4884¤cy_code=USD
Requires
- php: ^8
Requires (Dev)
- friendsofphp/php-cs-fixer: v3.0.0-beta.2
- infection/infection: ^0.21.5
- mikey179/vfsstream: ^1.6
- phpunit/phpunit: ^9.5
README
Installation
You can install the package via composer:
composer require tleckie/csv
Usage
Reader:
<?php include_once "vendor/autoload.php"; use Tleckie\Csv\Csv; $csv = new Csv('file.csv'); $reader = $csv->reader(); foreach($reader as $position => $row){ foreach($row as $index => $value){ // ... } // byIndex $row->byIndex(0); // hasIndex $row->hasIndex(0); // new instance with reversed data $row->reverse(); // array $row->toArray(); // preserve keys $row->removeByIndex(1); // removeFirst and preserve keys $row->removeFirst(); // removeLast $row->removeLast(); // countable count($row) or $row->count() $row->count(); count($row); }
Explicit reader
use Tleckie\Csv\Reader; $csv = new Reader('file.csv', ',', '|');
Writer
use Tleckie\Csv\Csv; use Tleckie\Csv\Row; $csv = new Csv('file.csv', ',', '"'); $writer = $csv->writer(); $writer->writeLine([1,2,3,4,5,"Test comma, separated"]); $writer->writeLine(new Row([1,2,3,4,5,"Test comma, separated"]));
Explicit writer
use Tleckie\Csv\Writer; use Tleckie\Csv\Row; $writer = new Writer('file.csv', ',', '|','\\', 'w'); $writer->writeLine([1,2,3,4,5,"Test comma, separated"]); $writer->writeLine(new Row([1,2,3,4,5,"Test comma, separated"]));