A simple PHP library to parse and generate CSV files.

Simon Vieille f8731f6fbe CI 11 months ago
src 37be0f572f Cleaning code 11 months ago
tests 37be0f572f Cleaning code 11 months ago
.gitignore 4809f0157b Insight analyse fixes 11 months ago
LICENSE 4809f0157b Insight analyse fixes 11 months ago
README.md f8731f6fbe CI 11 months ago
composer.json b806c85932 composer 1 year ago
phpci.yml 6c4a535ecd Tests 11 months ago
phpunit.xml 96c829967f test 2 years ago

README.md

CSV parser/generator

A simple PHP library to parse and generate CSV files.

Composer installation

$ composer require deblan/csv

Or in your composer.json:

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

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);

Parser

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();