Once your object is instantiated you can optionally set several CSV properties. The following methods works on both the Reader and the Writer class.
$csv->setDelimiter(';');
$delimiter = $csv->getDelimiter(); //returns ";"
The default delimiter character is ,.
$csv->setEnclosure('|');
$enclosure = $csv->getEnclosure(); //returns "|"
The default enclosure character is ".
$csv->setEscape('\\');
$escape = $csv->getEscape(); //returns "\"
The default escape character is \.
League\Csv objects rely internally on the SplFileObject class. In order to fine tune the class behavior you can adjust the SplFileObject flags used.
$csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
$flags = $csv->getFlags(); //returns an integer
On instantiation the flags set are :
SplFileObject::READ_CSVSplFileObject::READ_AHEADSplFileObject::SKIP_EMPTYOn update you can add or remove any SplFileObject flags except for the SplFileObject::READ_CSV flag.
The method takes two arguments:
1);$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
If you are no sure about the delimiter you can ask the library to detect it for you using the detectDelimiterList method.
The method takes two arguments:
1);",", ";", "\t");$reader = Reader::createFromPath('/path/to/your/csv/file.csv', 'r');
$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.
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.
The following properties only affect the CSV when you are writing or saving data to it.
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.
$csv->setNewline("\r\n");
$newline = $csv->getNewline(); //returns "\r\n"
The default newline sequence is \n;
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.
$bom = $csv->getInputBOM();
You can of course set the outputting BOM you want your CSV to be associated with.
$csv->setOutputBOM(Reader::BOM_UTF8);
$bom = $csv->getOutputBOM(); //returns "\xEF\xBB\xBF"
The default output BOM character is set to null.
The following properties and method only works when converting CSV document into other available format.
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:
$reader->setEncodingFrom('iso-8859-15');
echo $reader->getEncodingFrom(); //returns iso-8859-15;
By default getEncodingFrom returns UTF-8 if setEncodingFrom was not used.
$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
How can I help you explore Laravel packages today?