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

Force field enclosure

Available since version 9.1.0

class EncloseField extends php_user_filter
    public static function addTo(Writer $csv, string $sequence): Writer
    public static function getFiltername(): string
    public static function register(): void

The EnclosureField force the Writer class to enclose all its record fields.

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

Usage with Writer objects


public static EnclosureField::addTo(Writer $csv, string $sequence): Writer

The EnclosureField::addTo method will:


use League\Csv\EnclosureField;
use League\Csv\Writer;

$writer = Writer::createFromPath('php://temp');
EnclosureField::addTo($writer, "\t\x1f"); //adding the stream filter to force enclosure
$writer->output('mycsvfile.csv'); //outputting a CSV Document with all its field enclosed

The $sequence argument should be a sequence containing at least one character that forces fputcsv to enclose the field value. If not, an InvalidArgumentException exception will be thrown.

Usage with PHP stream resources


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

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


use League\Csv\EnclosureField;


$sequence = "\t\x1f";

$resource = fopen('/path/to/my/file', 'r+');
$filter = stream_filter_append($resource, EnclosureField::getFiltername(), STREAM_FILTER_WRITE, [
    'sequence' => $sequence,

$record = array_map(function ($value) use ($sequence) {
	return $sequence.$value;
}; $record);

fputcsv($resource, $record);