Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Csv Laravel Package

league/csv

View on GitHub
Deep Wiki
Context7

layout: default title: Setting and Accessing CSV settings redirect_from: /properties/

CSV properties

Once your object is instantiated you can optionally set several CSV properties. The following methods works on both the Reader and the Writer class.

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

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

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

Accessing and Setting CSV properties

The CSV delimiter character

Description

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

Example

use League\Csv\Reader;

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

Notes

The default delimiter character is ,.

The enclosure character

Description

public AbstractCsv::setEnclosure(string $delimiter): AbstractCsv
public AbstractCsv::getEnclosure(void): string

Example

use League\Csv\Writer;

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

Notes

The default enclosure character is ".

The escape character

Description

public AbstractCsv::setEscape(string $delimiter): AbstractCsv
public AbstractCsv::getEscape(void): string

Example

use League\Csv\Reader;

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

Notes

The default escape character is \.

fetchDelimitersOccurrence

This method allow you to find the occurrences of some delimiters in a given CSV object.

public AbstractCsv::fetchDelimitersOccurrence(
    array $delimiters,
    int $nbRows = 1
): array

The method takes two arguments:

  • an array containing the delimiters to check;
  • an integer which represents the number of rows to scan (defaults to 1);
use League\Csv\Reader;

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

$delimiters_list = $reader->fetchDelimitersOccurrence([' ', '|'], 10);
// $delimiters_list can be the following
// [
//     '|' => 20,
//     ' ' => 0,
// ]
// This seems to be a consistent CSV with:
// - the delimiter "|" appearing 20 times in the 10 first rows
// - the delimiter " " never appearing

Writing mode only properties

The following properties only affect the CSV object when you are writing and/or saving data to it.

The newline sequence

To improve interoperability with programs interacting with CSV, the newline sequence is appended to each CSV newly inserted line.

Description

public AbstractCsv::setNewline(string $sequence): AbstractCsv
public AbstractCsv::getNewline(void): string

Example

use League\Csv\Writer;

$csv = Writer::createFromPath('/path/to/file.csv');
$csv->setNewline("\r\n");
$newline = $csv->getNewline(); //returns "\r\n"

Notes

The default newline sequence is \n;

The BOM sequence

To improve interoperability with programs interacting with CSV, you can manage the presence of the BOM sequence in your CSV content.

Detect the currently used BOM sequence

public AbstractCsv::getInputBOM(void): string

Detect the current BOM character is done using the getInputBOM method. This method returns the currently used BOM character or an empty string if none is found or recognized.

use League\Csv\Writer;

$csv = Writer::createFromPath('/path/to/file.csv');
$bom = $csv->getInputBOM();

Set the outputting BOM sequence

public AbstractCsv::setOutputBOM(string $sequence): AbstractCsv
public AbstractCsv::getOutputBOM(void): string
  • setOutputBOM: sets the outputting BOM you want your CSV to be associated with.
  • getOutputBOM: get the outputting BOM you want your CSV to be associated with.
use League\Csv\Reader;

$csv = Reader::createFromPath('/path/to/file.csv', 'r');
$csv->setOutputBOM(Reader::BOM_UTF8);
$bom = $csv->getOutputBOM(); //returns "\xEF\xBB\xBF"

Notes

  • The default output BOM character is set to an empty string.
  • The AbstractCsv class provide constants to ease BOM sequence manipulation.

Conversion only properties

To convert your CSV document into another format it must be encoded in UTF-8.

When this is not the case, you should transcode it first using the library stream filtering mechanism. When this is not applicable you should provide the CSV original encoding charset to the CSV object using the following methods.

methods

public AbstractCsv::setInputEncoding(string $sequence): AbstractCsv
public AbstractCsv::getInputEncoding(void): string
public AbstractCsv::setEncodingFrom(string $sequence): AbstractCsv
public AbstractCsv::getEncodingFrom(void): string
  • AbstractCsv::setEncodingFrom is replaced by AbstractCsv::setInputEncoding
  • AbstractCsv::getInputEncoding is replaced by AbstractCsv::getEncodingFrom

Example

use League\Csv\Reader;

$csv = Reader::createFromPath('/path/to/file.csv', 'r');
$csv->setInputEncoding('iso-8859-15');
echo $csv->getInputEncoding(); //returns iso-8859-15;

Notes

By default getInputEncoding returns UTF-8 if setInputEncoding was not used.

use League\Csv\Reader;

$reader = Reader::createFromFileObject(new SplFileObject('/path/to/bengali.csv'));
//we are using the setInputEncoding method to transcode the CSV into UTF-8
$reader->setInputEncoding('iso-8859-15');
echo json_encode($reader);
//the CSV is transcoded from iso-8859-15 to UTF-8
//before being converted to JSON format;
echo $reader; //outputting the data is not affected by the conversion
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport