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: Csv character controls description: Setting and Getting CSV document character controls to allow CSV document parsing

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

The getDelimiter() and setDelimiter() methods set and returns the CSV delimiter character used by the underlying class. The setter method expects a single byte character string to be used.

use League\Csv\Reader;

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

The enclosure character

The getEnclosure() and setEnclosure() methods set and returns the CSV enclosure character used by the underlying class. The setter method expects a single byte character string to be used.

use League\Csv\Writer;

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

The escape character

The getEscape() and setEscape() methods set and returns the CSV escape character used by the underlying class. The setter method expects a single byte character string to be used.

This is a PHP specific control character that 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.

use League\Csv\Reader;

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

$csv = Reader::from('/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::from($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

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::from('/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.

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