The library is composed of two main classes:
League\Csv\Reader to read data from a CSVLeague\Csv\Writer to write new data into a CSVBoth classes extend the League\Csv\AbstractCsv class and as such share methods for instantiation.
If your CSV document was created or is read on a Macintosh computer, 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...
Because CSVs come in different forms we used named constructors to offer several ways to instantiate the library objects.
This named constructor will create a new object à la fopen.
public static AbstractCsv::createFromPath(
mixed $path,
string $open_mode = 'r+'
): AbstractCsv
$path parameter can be:
SplFileInfo object, the string path will be fetch from the object public methods.__toString method the path will be the object string representation.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'!!
Instantiate a new Csv object from a SplFileObject.
public static AbstractCsv::createFromFileObject(SplFileObject $obj): AbstractCsv
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());
This named constructor will create a new object from a given string.
public static AbstractCsv::createFromString(mixed $str): AbstractCsv
This method accepts only one single parameter, an object implementing the __toString method or a string.
use League\Csv\Reader;
use League\Csv\Writer;
$reader = Reader::createFromString('john,doe,john.doe@example.com');
$writer = Writer::createFromString('john,doe,john.doe@example.com');
This named constructor will create a new object from a stream resource.
public static AbstractCsv::createFromStream(resource $stream): AbstractCsv
This method accepts only one single parameter, a resource stream. The resource stream MUST be seekable otherwise a InvalidArgumentException will be thrown.
use League\Csv\Reader;
use League\Csv\Writer;
$reader = Reader::createFromStream(fopen('/path/to/the/file.csv', 'r+'));
$writer = Writer::createFromStream(fopen('php://temp', 'r+'));
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.
newReader to create a new League\Csv\Reader object;newWriter to create a new League\Csv\Writer object;Both methods accept an optional $open_mode parameter.
public AbstractCsv::newReader(string $open_mode = 'r+'): Reader
public AbstractCsv::newWriter(string $open_mode = 'r+'): Writer
$open_mode default value is r+ for both methods.$open_mode parameter 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+');
How can I help you explore Laravel packages today?