Instantiation
The library is composed of two main classes:
League\Csv\Readerto read data from a CSVLeague\Csv\Writerto write new data into a CSV
Both classes extend the League\Csv\AbstractCsv class and as such share methods for instantiation.
Mac OS Server
If you are on a Mac OS X Server, add the following lines before using the library to help PHP detect line ending in Mac OS X.
if (! ini_get("auto_detect_line_endings")) {
ini_set("auto_detect_line_endings", '1');
}
//the rest of the code continues here...
Instantiating a new CSV object
Because CSVs come in different forms we used named constructors to offer several ways to instantiate the library objects.
createFromPath($path, $open_mode)
This named constructor will create a new object à la fopen:
- The
$pathparameter can be:- a
SplFileInfoobject, the string path will be fetch from the object public methods. - an object implementing the
__toStringmethod the path will be the object string representation. - a string.
- a
- The
$open_modeparameter which defaults tor+if none is supplied.
The resulting string and $open_mode parameters are used to lazy load internally a SplFileObject object.
use League\Csv\Reader;
use League\Csv\Writer;
$reader = Reader::createFromPath('/path/to/your/csv/file.csv', 'r');
//the $reader object will use the 'r+' open mode as no `open_mode` parameter was supplied.
$writer = Writer::createFromPath(new SplFileObject('/path/to/your/csv/file.csv', 'a+'), 'w');
//the $writer object open mode will be 'w'!!
createFromFileObject(SplFileObject $obj)
If you have a SplFileObject and you want to directly work with it you should use the createFromFileObject named constructor. This method accepts only one single parameter, a SplFileObject object.
use League\Csv\Reader;
use League\Csv\Writer;
$reader = Reader::createFromFileObject(new SplFileObject('/path/to/your/csv/file.csv'));
$writer = Writer::createFromFileObject(new SplTempFileObject());
createFromString($str, $newline = “\n”)
If you have a raw CSV string use the createFromString named constructor. This method accepts two parameters:
- the raw CSV string;
- the newline sequence to be added at the end of the raw CSV string;
If no newline sequence is specified, the newline sequence used will be \n to match the one added by PHP fputcsv function.
use League\Csv\Reader;
use League\Csv\Writer;
$reader = Reader::createFromString('john,doe,john.doe@example.com', "\n");
$writer = Writer::createFromString('john,doe,john.doe@example.com', "\r\n");
Switching from one class to the other
At any given time you can switch or create a new League\Csv\Writer or a new League\Csv\Reader from the current object. To do so, you can use the following methods.
- the
newReaderto create a newLeague\Csv\Readerobject; - the
newWriterto create a newLeague\Csv\Writerobject;
Both methods accept an optional $open_mode parameter.
- When not explicitly set, the
$open_modedefault value isr+for both methods. - If the initial object
$open_modeparameter was not taken into account any new CSV object created with these methods won’t take into account the given$open_mode.
$reader = $writer->newReader('r+');
$newWriter = $reader->newWriter('a');
$anotherWriter = $newWriter->newWriter('r+');