The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Connections Settings

Inserting Records

Selecting Records

Interoperability

Converting Records

Upgrading Guide

CSV character controls

To correctly parse a CSV document you are required to set the character controls to be used by the Reader or the Writer object.

On error the setter methods will throw a Exception exception if the submitted string length is not equal to 1.

The delimiter character.

Description

<?php

public AbstractCsv::setDelimiter(string $delimiter): self
public AbstractCsv::getDelimiter(void): string

Example

<?php

use League\Csv\Reader;

$csv = Reader::createFromPath('/path/to/file.csv', 'r');
$csv->setDelimiter(';');
$delimiter = $csv->getDelimiter(); //returns ";"

The default delimiter character is ,.

The enclosure character

Description

<?php

public AbstractCsv::setEnclosure(string $enclosure): self
public AbstractCsv::getEnclosure(void): string

Example

<?php

use League\Csv\Writer;

$csv = Writer::createFromPath('/path/to/file.csv');
$csv->setEnclosure('|');
$enclosure = $csv->getEnclosure(); //returns "|"

The default enclosure character is ".

The escape character

Description

<?php

public AbstractCsv::setEscape(string $escape): self
public AbstractCsv::getEscape(void): string

Example

<?php

use League\Csv\Reader;

$csv = Reader::createFromPath('/path/to/file.csv', 'r');
$csv->setEscape('\\');
$escape = $csv->getEscape(); //returns "\"

The default escape character is \.

To produce RFC4180 compliant CSV documents you should use the RFC4180Field stream filter to work around bugs associated with the use of the escape character.

Inherited character controls

When using a SplFileObject, the returned AbstractCsv object will inherit the object underlying CSV controls.

<?php

$file = new SplTempFileObject();
$file->setFlags(SplFileObject::READ_CSV);
$file->setCsvControl('|');

$csv = Reader::createFromFileObject($file);

echo $csv->getDelimiter(); //display '|'

Detecting the delimiter character

<?php

function League\Csv\delimiter_detect(Reader $csv, array $delimiters, $limit = 1): array

The delimiter_detect function helps detect the possible delimiter character used by the CSV document. This function returns the number of CSV fields found in the document depending on the submitted delimiters given.

The function takes three (3) arguments:

and returns an associated array whose keys are the submitted delimiters characters and whose values represents the field numbers found depending on the delimiter value.

<?php

use function League\Csv\delimiter_detect;
use League\Csv\Reader;

$reader = Reader::createFromPath('/path/to/file.csv', 'r');
$reader->setEnclosure('"');
$reader->setEscape('\\');

$result = delimiter_detect($reader, [' ', '|'], 10);
// $result can be the following
// [
//     '|' => 20,
//     ' ' => 0,
// ]
// This seems to be a consistent CSV with:
// - 20 fields were counted with the "|" delimiter in the 10 first records;
// - in contrast no field was detected for the " " delimiter;

If the submitted delimiter is invalid or not found in the document, 0 will be returned as its associated value.

To detect the delimiters stats on the full CSV document you need to set $limit to -1.

This function only returns hints. Only the CSV providers will validate the real CSV delimiter character.

This function only test the delimiters you gave it.