The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Connections

Inserting Records

Selecting Records

Converting Records

Upgrading Guide

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

CSV character controls

<?php

public AbstractCsv::getDelimiter(void): string
public AbstractCsv::getEnclosure(void): string
public AbstractCsv::getEscape(void): string
public AbstractCsv::setDelimiter(string $delimiter): self
public AbstractCsv::setEnclosure(string $enclosure): self
public AbstractCsv::setEscape(string $escape): self

To correclty 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

<?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');
$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

Warning: The library does not attempt to workaround a reported PHP bug, Data using the escape character are correctly escaped but the escape character is not removed from the CSV content.

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');
$csv->setEscape('\\');
$escape = $csv->getEscape(); //returns "\"

The default escape character is \.

Inherited character controls

When using a SplFileObject, the underlying CSV controls from the submitted object are inherited by the return AbstractCsv object.

<?php

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

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

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

The escape character is only inherited starting with PHP 7.0.10.