Once your object is instantiated you can optionally set several CSV properties. The following methods works on both the
Reader and the
Accessing and Setting CSV properties
The delimiter character
<?php $csv->setDelimiter(';'); $delimiter = $csv->getDelimiter(); //returns ";"
The default delimiter character is
The enclosure character
<?php $csv->setEnclosure('|'); $enclosure = $csv->getEnclosure(); //returns "|"
The default enclosure character is
The escape character
<?php $csv->setEscape('\\'); $escape = $csv->getEscape(); //returns "\"
The default escape character is
The SplFileObject flags
League\Csv objects rely internally on the
SplFileObject class. In order to fine tune the class behavior you can adjust the SplFileObject flags used.
<?php $csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY); $flags = $csv->getFlags(); //returns an integer
- On instantiation the flags set are :
- On update you can add or remove any
SplFileObjectflags except for the
Detecting CSV delimiter
fetchDelimitersOccurrence(array $delimiters, $nbRows = 1)
The method takes two arguments:
- an array containing the delimiters to check;
- an integer which represents the number of rows to scan (default to
<?php $reader = Reader::createFromPath('/path/to/your/csv/file.csv'); $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
detectDelimiterList($nbRows = 1, array $delimiters = )
If you are no sure about the delimiter you can ask the library to detect it for you using the
The method takes two arguments:
- the number of rows to scan (default to
- the possible delimiters to check (you don’t need to specify the following delimiters as they are already checked by the method:
",", ";", "\t");
<?php $reader = Reader::createFromPath('/path/to/your/csv/file.csv'); $reader->setEnclosure('"'); $reader->setEscape('\\'); $reader->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY); $delimiters_list = $reader->detectDelimiterList(10, [' ', '|']); // $delimiters_list can be the following // [ // 20 => '|', // 3 => ';' // ] // This is a inconsistent CSV with: // - the delimiter "|" appearing 20 times in the 10 first rows // - the delimiter ";" appearing 3 times in the 10 first rows
The more rows and delimiters you add, the more time and memory consuming the operation will be. The method returns an
array of the delimiters found.
- If a single delimiter is found the array will contain only one delimiter;
- If multiple delimiters are found the array will contain the found delimiters sorted descendingly according to their occurences in the defined rows set;
- If no delimiter is found or your CSV is composed of a single column, the array will be empty;
Whenever a user creates a new CSV object using the
newWriter or the
newReader methods, the current CSV object properties are copied to the new instance.
Writing mode only properties
The following properties only affect the CSV when you are writing or saving data to it.
The newline sequence
The newline sequence is appended to each CSV newly inserted line. To improve interoperability with programs interacting with CSV and because the php
fputcsv implementation has a hardcoded
"\n", we need to be able to replace this last
LF code with one supplied by the developer.
<?php $csv->setNewline("\r\n"); $newline = $csv->getNewline(); //returns "\r\n"
The default newline sequence is
The BOM character
To improve interoperability with programs interacting with CSV, you can now manage the presence of a BOM character in your CSV content.
Detect the current BOM character is done using the
getInputBOM method. This method returns the currently used BOM character or
null if none is found or recognized.
<?php $bom = $csv->getInputBOM();
You can of course set the outputting BOM you want your CSV to be associated with.
<?php $csv->setOutputBOM(Reader::BOM_UTF8); $bom = $csv->getOutputBOM(); //returns "\xEF\xBB\xBF"
The default output
BOM character is set to
Conversion only properties
The following properties and method only works when converting CSV document into other available format.
The encoding charset
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 using the library stream filtering mechanism.
When this is not applicable you can fallback by providing the CSV original encoding charset to the CSV class using the following method:
<?php $reader->setEncodingFrom('iso-8859-15'); echo $reader->getEncodingFrom(); //returns iso-8859-15;
setEncodingFrom was not used.
<?php $reader = Reader::createFromFileObject(new SplFileObject('/path/to/bengali.csv')); //we are using the setEncodingFrom method to transcode the CSV into UTF-8 $reader->setEncodingFrom('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