The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

CSV

CSV data manipulation made easy in PHP.

$ composer require league/csv

Releases

Current Stable

league/csv:^9.0

  • Requires: PHP >= 7.0.10
  • Latest: 9.0.1 - 2017-08-21
  • Supported Until: TBD

Full Documentation

Unsupported

league/csv:^7.0

  • Requires: PHP >= 5.4.0
  • Latest: 7.2.0 - 2015-11-02
  • Supported Until: 2016-06-11

Full Documentation

Once a new major version is released, the previous stable release remains supported for six more months through patches and security fixes.

Highlights

The library was designed for developers who want to deal with CSV data using modern code and without the high levels of bootstrap and low-levels of usefulness provided by existing core functions or third party-code.

  1. Simple API

  2. Read and Write to CSV documents in a memory efficient and scalable way

  3. Support PHP Stream filtering capabilities

  4. Transform CSV documents into popular formats (JSON, XML or HTML)

  5. Framework-agnostic

Features

Accessing CSV documents records

Reader, the read only connection object enables accessing CSV records easily

<?php
use League\Csv\Reader;

//load the CSV document from a file path
$csv = Reader::createFromPath('/path/to/your/csv/file.csv');
$csv->setHeaderOffset(0);

$header = $csv->getHeader(); //returns the CSV header record
$records = $csv->getRecords(); //returns all the CSV records as an Iterator object

echo $csv; //returns the CSV document as a string

Adding new CSV records is made simple

Writer, the write only connection object enables adding one or more records in one call.

<?php

use League\Csv\Writer;

$header = ['first name', 'last name', 'email'];
$records = [
    [1, 2, 3],
    ['foo', 'bar', 'baz'],
    ['john', 'doe', 'john.doe@example.com'],
];

//load the CSV document from a string
$csv = Writer::createFromString('');

//insert the header
$csv->insertOne($header);

//insert all the records
$csv->insertAll($records);

echo $csv; //returns the CSV document as a string

Advanced CSV records selection

Statement, the constraint builder object ease CSV records selection

<?php
use League\Csv\Reader;
use League\Csv\Statement;

//load the CSV document from a stream
$stream = fopen('/path/to/your/csv/file.csv', 'r');
$csv = Reader::createFromStream($stream);
$csv->setDelimiter(';');
$csv->setHeaderOffset(0);

//build a statement
$stmt = (new Statement())
    ->offset(10)
    ->limit(25);

//query your records from the document
$records = $stmt->process($csv);
foreach ($records as $record) {
    //do something here
}

CSV documents converters

Different converters objects ease transforming your CSV documents into other popular formats

<?php

use League\Csv\Reader;
use League\Csv\XMLConverter;

//load the CSV document from a SplFileObject
$file = new SplFileObject('/path/to/your/csv/file.csv', 'r');
$csv = Reader::createFromFileObject($file);

$converter = (new XMLConverter())
    ->rootElement('csv')
    ->recordElement('record', 'offset')
    ->fieldElement('field', 'name');

$dom = $converter->convert($csv);
$dom->formatOutput = true;
$dom->encoding = 'iso-8859-15';

echo '<pre>', PHP_EOL;
echo htmlentities($dom->saveXML());
// <?xml version="1.0" encoding="iso-8859-15"?>
// <csv>
//   ...
//   <record offset="71">
//     <field name="prenoms">Anaïs</field>
//     <field name="nombre">137</field>
//     <field name="sexe">F</field>
//     <field name="annee">2004</field>
//   </record>
//   ...
//   <record offset="1099">
//     <field name="prenoms">Anaïs</field>
//     <field name="nombre">124</field>
//     <field name="sexe">F</field>
//     <field name="annee">2005</field>
//   </record>
// </csv>

Supports PHP Stream filter API

PHP stream filters can directly be used to ease manipulating CSV document

<?php

use League\Csv\Reader;

$csv = Reader::createFromPath('/path/to/your/csv/file.csv');
$csv->setHeaderOffset(0);

$input_bom = $csv->getInputBOM();

if ($input_bom === Reader::BOM_UTF16_LE || $input_bom === Reader::BOM_UTF16_BE) {
    $csv->addStreamFilter('convert.iconv.UTF-16/UTF-8');
}

foreach ($csv as $record) {
    //all fields from the record are converted from UTF-16 into UTF-8 charset
    //and the BOM sequence is removed
}

Questions?

CSV was created by Ignace Nyamagana Butera. Find him on Twitter at @nyamsprod.