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.

The delimiter character

Description

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

Example

use League\Csv\Reader;

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

The default delimiter character is ,.

setDelimiter will throw a Exception exception if the submitted string length is not equal to 1 byte.

The enclosure character

Description

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

Example

use League\Csv\Writer;

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

The default enclosure character is ".

setEnclosure will throw a Exception exception if the submitted string length is not equal to 1 byte.

The escape character

This is a PHP specific control character which sometimes interferes with CSV parsing and writing. It is recommended in versions preceding 9.2.0 to never change its default value unless you do understand its usage and its consequences.

Starting with version PHP7.4 it is recommended to use the library with the escape parameter equal to the empty string. see Deprecation for PHP8.4 and CSV and PHP8.4 for more information.

Description

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

Example

use League\Csv\Reader;

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

The default escape character is \.

Since version 9.2.0 you can provide an empty string for the escape character to enable better RFC4180 compliance.

setEscape will throw a Exception exception if the submitted string length is not equal to 1 byte or an empty string.

use League\Csv\Reader;

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

Inherited character controls

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

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

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

echo $csv->getDelimiter(); //display '|'
echo $csv->getEnclosure(); //display "'"
echo $csv->getEscape();    //display '\'

Detecting the delimiter character

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

or

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

Since version 9.7 this function is deprecated and you are encouraged to use Info::getDelimiterStats instead.

The Info::getDelimiterStats static method 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:

  • a Reader object;
  • an array containing the delimiters to check;
  • an integer which represents the number of CSV records to scan (defaults to 1);

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

use League\Csv\Info;
use League\Csv\Reader;

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

$result = Info::getDelimiterStats($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 tests the delimiters you pass it.