1. Go to this page and download the library: Download csanquer/colibri-csv library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
csanquer / colibri-csv example snippets
use CSanquer\ColibriCsv\CsvReader;
// create the reader
$reader = new CsvReader(array(
'delimiter' => ';',
'enclosure' => '"',
'encoding' => 'CP1252',
'eol' => "\r\n",
'escape' => "\\",
'first_row_header' => false,
'bom' => false,
'translit' => 'translit',
'force_encoding_detect' => false,
'skip_empty' => false,
'trim' => false,
));
//Open the csv file to read
$reader->open('test.csv');
// or open an existing stream resource
$stream = fopen('test.csv', 'rb');
$reader->open($stream);
// or read an existing CSV string by creating a temporary in-memory file stream (not recommended for large CSV)
$reader->createTempStream('lastname,firstname,age
Martin,Durand,"28"
Alain,Richard,"36"
');
//Read each row
foreach ($reader as $row) {
// do what you want with the current row array : $row
}
// or get all rows in one call (not recommended for large CSV)
$csvRows = $reader->getRows();
//close the csv file stream
$reader->close();
use CSanquer\ColibriCsv\CsvWriter;
// create the writer
$writer = new CsvWriter(array(
'delimiter' => ';',
'enclosure' => '"',
'encoding' => 'CP1252',
'enclosing_mode' => 'minimal',
'escape_double' => true,
'eol' => "\r\n",
'escape' => "\\",
'bom' => false,
'translit' => 'translit',
'first_row_header' => false,
'trim' => false,
));
//Open the csv file to write
$writer->open('test.csv');
// or open an existing stream resource
$stream = fopen('test.csv', 'wb');
$writer->open($stream);
// or create an empty temporary in-memory file stream to write in and get CSV text later
// (not recommended for large CSV file)
$writer->createTempStream();
//Write a row
$writer->writeRow(array('a', 'b', 'c'));
//Write multiple rows at the same time
$writer->writeRows(array(
array('d', 'e', 'f'),
array('g', 'h', 'i'),
array('j', 'k', 'l'),
));
// get the CSV Text as plain string
$writer->getFileContent();
//close the csv file
$writer->close();
use CSanquer\ColibriCsv\Dialect;
use CSanquer\ColibriCsv\CsvReader;
use CSanquer\ColibriCsv\CsvWriter;
// create a dialect with some CSV parameters
$dialect = new Dialect(array(
'delimiter' => ';',
'enclosure' => '"',
'enclosing_mode' => 'minimal',
'encoding' => 'CP1252',
'eol' => "\r\n",
'escape' => "\\",
'escape_double' => true,
'bom' => false,
'translit' => 'translit',
'force_encoding_detect' => false,
'skip_empty' => false,
'trim' => false,
);
// change a parameter
$dialect->setLineEndings("\n");
// create the reader
$reader = new CsvReader($dialect);
//or a writer
$writer = new CsvWriter($dialect);