The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Connections Settings

Inserting Records

Selecting Records

Converting Records

Upgrading Guide

This is the documentation for the upcoming version 9.0. This is a work in progress

Document interoperability

Depending on your operating system and on the software you are using to read/import your CSV you may need to adjust the encoding character and add its corresponding BOM character to your CSV.

Out of the box, League\Csv connections do not alter the CSV document original encoding.

In the examples below we will be using an existing CSV in ISO-8859-15 charset encoding as a starting point. The code will vary if your CSV document is in a different charset.

MS Excel on Windows

On Windows, MS Excel, expects an UTF-8 encoded CSV with its corresponding BOM character. To fullfill this requirement, you simply need to add the UTF-8 BOM character if needed as explained below:

<?php

use League\Csv\BOM;
use League\Csv\Reader;

$reader = Reader::createFromPath('/path/to/my/file.csv')
    ->setOutputBOM(BOM::UTF8)
    ->addStreamFilter('convert.iconv.ISO-8859-15/UTF-8')
;
//BOM detected and adjusted for the output
echo $reader->__toString();

MS Excel on MacOS

On a MacOS system, MS Excel requires a CSV encoded in UTF-16 LE using the tab character as delimiter. Here’s an example on how to meet those requirements using the League\Csv package.

<?php

use League\Csv\BOM;
use League\Csv\Reader;
use League\Csv\Writer;

//the current CSV is ISO-8859-15 encoded with a ";" delimiter
$origin = Reader::createFromPath('/path/to/french.csv', 'r')
    ->setDelimiter(';')
;

//let's use stream resource
//let's set the output BOM
//we set the tab as the delimiter character
$writer = Writer::createFromStream(fopen('php://temp', 'r+'))
    ->setOutputBOM(BOM::UTF16_LE)
    ->setDelimiter("\t")
    ->addStreamFilter('convert.iconv.ISO-8859-15/UTF-16')
;

//we insert csv data
$writer->insertAll($origin);
//all is good let's output the results
$writer->output('mycsvfile.csv');

In the examples aboves, we assumed that the iconv extension is present. Alternatively your can use the League\CharsetConverter class to register your PHP stream filter.