A simple PHP library to parse and generate CSV files.
Ir al archivo
Simon Vieille e1cb3d8b9a
ci/woodpecker/push/woodpecker Pipeline was successful Detalles
ci/woodpecker/manual/woodpecker Pipeline was successful Detalles
update ci
2022-07-26 10:11:21 +02:00
src/Deblan/Csv add stream parser 2020-02-10 16:24:43 +01:00
tests tests for php7 2020-02-10 16:24:56 +01:00
.gitignore Insight analyse fixes 2017-03-13 13:37:18 +01:00
.woodpecker.yml update ci 2022-07-26 10:06:58 +02:00
LICENSE Insight analyse fixes 2017-03-13 13:37:18 +01:00
README.md update ci 2022-07-26 10:11:21 +02:00
composer.json composer 2016-04-04 09:56:00 +02:00
phpunit.xml add stream parser 2020-02-10 16:24:43 +01:00

README.md

CSV parser/generator

Build Status

A simple PHP library to:

  • parse a CSV file
  • parse a stream as CSV datas
  • generate CSV files.

PHP >= 7.1 required.

Composer installation

$ composer require deblan/csv "~3"

Or in your composer.json:

{
    "require": {
        "deblan/csv": "~3"
    }
}

Usages

Generator

use Deblan\Csv\Csv;

$csv = new Csv();

// Defines the delimiter (default is ;)
$csv->setDelimiter(";");

// Defines the enclosure (default is ")
$csv->setEnclosure('"');

// Defines the end of line (default is \n)
$csv->setEndOfLine("\n");

// Defines the charset (default is UTF-8)
$csv->setCharset("UTF-8");

// Add a new line at the end
$csv->addData(['Foo', '$1000'));

// Add a new line at the end
$csv->appendData(['Bar', '$600']);

// Add a new line at the beginning
$csv->prependData(['Boo', '$3000']);

// Defines all the datas
$csv->setDatas([[...], [...]]);

// Defines the header
$csv->setHeaders(['Product', 'Price']);

// Rendering
$result = $csv->render();

// Rendering to a file
$result = $csv->render('products.csv');

// Appending to a file
$result = $csv->render('products.csv', FILE_APPEND);

Parse a file

use Deblan\Csv\CsvParser;

$csv = new CsvParser();

// Defines the delimiter (default is ;)
$csv->setDelimiter(";");

// Defines the enclosure (default is ")
$csv->setEnclosure('"');

// Defines the end of line (default is \n)
$csv->setEndOfLine("\n");

// Headers?
$csv->setHasHeaders(true);

// Parse a file
$csv->parseFile('products.csv');

// Parse a string
$csv->parseString($myString);

// Headers and datas
$headers = $csv->getHeaders();
$products = $csv->getDatas();

Parse a stream

use Deblan\Csv\CsvStreamParser;

// CsvStreamParser is a CsvParser
$csv = new CsvStreamParser();

// Parse a stream
$csv->parseStream(fopen('products.csv', 'r'));

while ($data = $csv->getData()) {
	// ...
}