The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Connections Settings

Inserting Records

Selecting Records


Converting Records

Upgrading Guide

This is the documentation for the upcoming version 9.0. This is a work in progress

RFC4180 Field compliance

class RFC4180Field extends php_user_filter
    public static function addTo(AbstractCsv $csv): AbstractCsv
    public static function getFiltername(): string
    public static function register(): void

The RFC4180Field class enables to work around the following bugs in PHP’s native CSV functions:

When using this stream filter you can easily create or read a RFC4180 compliant CSV document using League\Csv connections objects.

Changing the CSV objects control characters after registering the stream filter may result in unexpected returned records.

Usage with CSV objects


public static RFC4180Field::addTo(AbstractCsv $csv): AbstractCsv

The RFC4180Field::addTo method will register the stream filter if it is not already the case and add the stream filter to the CSV object using the following properties:

On records insertion

To fully comply with RFC4180 you will also need to use League\Csv\Writer::setNewline method.


use League\Csv\RFC4180Field;
use League\Csv\Writer;

$writer = Writer::createFromPath('php://temp');
$writer->setNewline("\r\n"); //RFC4180 Line feed
RFC4180Field::addTo($writer); //adding the stream filter to fix field formatting
$writer->output('mycsvfile.csv'); //outputting a RFC4180 compliant CSV Document

On records extraction

Conversely, to read a RFC4180 compliant CSV document, when using the League\Csv\Reader object, just need to add the League\Csv\RFC4180Field stream filter as shown below:


use League\Csv\Reader;
use League\Csv\RFC4180Field;

//the current CSV is ISO-8859-15 encoded with a ";" delimiter
$csv = Reader::createFromPath('/path/to/rfc4180-compliant.csv');
RFC4180Field::addTo($csv); //adding the stream filter to fix field formatting

foreach ($csv as $record) {
    //do something meaningful here...

Usage with PHP stream resources


public static RFC4180Field::register(): void
public static RFC4180Field::getFiltername(): string

To use this stream filter outside League\Csv objects you need to:


use League\Csv\RFC4180Field;


$resource = fopen('/path/to/my/file', 'r');
$filter = stream_filter_append($resource, RFC4180Field::getFiltername(), STREAM_FILTER_READ, [
    'enclosure' => '"',
    'escape' => '\\',
    'mode' => STREAM_FILTER_READ,

while (false !== ($record = fgetcsv($resource))) {
    //$records correctly parsed