csv/README.md

121 lines
2 KiB
Markdown
Raw Normal View History

2015-03-09 18:50:07 +01:00
CSV parser/generator
====================
2020-09-01 15:41:49 +02:00
[![Build Status](https://ci.gitnet.fr/buildStatus/icon?job=Gitnet%2Fcsv%2Fv3)](https://ci.gitnet.fr/job/Gitnet/job/csv/job/v3/)
2017-03-12 17:57:30 +01:00
2020-02-10 16:24:43 +01:00
A simple PHP library to:
* parse a CSV file
* parse a stream as CSV datas
* generate CSV files.
PHP >= 7.1 required.
2015-03-09 18:50:07 +01:00
2015-03-09 20:47:59 +01:00
## Composer installation
```
2020-02-10 16:24:43 +01:00
$ composer require deblan/csv "~3"
2015-03-09 20:47:59 +01:00
```
Or in your composer.json:
```
{
"require": {
2020-02-10 16:24:43 +01:00
"deblan/csv": "~3"
2015-03-09 20:47:59 +01:00
}
}
```
## Usages
2015-03-09 18:40:19 +01:00
2015-03-09 20:29:21 +01:00
### Generator
```php
use Deblan\Csv\Csv;
2015-03-09 20:31:04 +01:00
$csv = new Csv();
2015-03-09 20:29:21 +01:00
2017-03-12 15:20:09 +01:00
// Defines the delimiter (default is ;)
$csv->setDelimiter(";");
2015-03-09 20:29:21 +01:00
2017-03-12 15:20:09 +01:00
// Defines the enclosure (default is ")
$csv->setEnclosure('"');
2015-03-09 20:29:21 +01:00
2017-03-12 15:20:09 +01:00
// Defines the end of line (default is \n)
$csv->setEndOfLine("\n");
2015-03-09 20:29:21 +01:00
2017-03-12 15:20:09 +01:00
// 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
2017-03-12 17:57:30 +01:00
$csv->setHeaders(['Product', 'Price']);
2017-03-12 15:20:09 +01:00
// Rendering
$result = $csv->render();
2015-03-09 20:29:21 +01:00
2017-03-12 15:20:09 +01:00
// Rendering to a file
2017-03-12 17:57:30 +01:00
$result = $csv->render('products.csv');
2015-03-09 20:29:21 +01:00
2017-03-12 15:20:09 +01:00
// Appending to a file
2017-03-12 17:57:30 +01:00
$result = $csv->render('products.csv', FILE_APPEND);
2015-03-09 20:29:21 +01:00
```
2020-02-10 16:24:43 +01:00
### Parse a file
2015-03-09 20:29:21 +01:00
```php
2015-03-09 20:31:04 +01:00
use Deblan\Csv\CsvParser;
2015-03-09 20:29:21 +01:00
2017-03-12 17:57:30 +01:00
$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);
2015-03-09 20:29:21 +01:00
2017-03-12 17:57:30 +01:00
// Headers and datas
$headers = $csv->getHeaders();
2015-03-09 20:31:04 +01:00
$products = $csv->getDatas();
2015-03-09 20:29:21 +01:00
```
2020-02-10 16:24:43 +01:00
2020-05-24 14:31:08 +02:00
### Parse a stream
2020-02-10 16:24:43 +01:00
```php
use Deblan\Csv\CsvStreamParser;
// CsvStreamParser is a CsvParser
$csv = new CsvStreamParser();
// Parse a stream
$csv->parseStream(fopen('products.csv', 'r'));
while ($data = $csv->getData()) {
// ...
}
```