Skip to content

Commit

Permalink
adding more bom examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 21, 2015
1 parent 1ea0964 commit 3d3e40a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
17 changes: 0 additions & 17 deletions examples/bom.php

This file was deleted.

45 changes: 45 additions & 0 deletions examples/bom_excel_macos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

error_reporting(-1);
ini_set('display_errors', '1');

use League\Csv\Reader;
use League\Csv\Writer;
use lib\FilterTranscode;

require '../vendor/autoload.php';

//the current CSV is UTF-8 encoded with a ";" delimiter
$csv = Reader::createFromPath(__DIR__.'/data/prenoms.csv');

//To be open in MacOS Excel a CSV must
// - be encoded in UTF16-LE
// - use tab delimiter

//let's convert the CSV to be UTF-16_LE encoded with a tab delimiter.

//we must use `createFromPath` to be able to use the stream capability
//we must use a temp file to be able to rewind the cursor file without loosing
//the modification
$writer = Writer::createFromPath('/tmp/toto.csv', 'w');
$writer->setNullHandlingMode(Writer::NULL_AS_EMPTY);

// we register a Transcode Filter class to convert the CSV into the proper encoding charset
stream_filter_register(FilterTranscode::FILTER_NAME."*", "\lib\FilterTranscode");
$writer->appendStreamFilter(FilterTranscode::FILTER_NAME."UTF-8:UTF-16LE");

//we set the tab as the delimiter character
$writer->setDelimiter("\t");

//we insert csv data
$writer->insertAll($csv);

//let's switch to the Reader object
//Writer::output will failed because of the open mode
$reader = $writer->newReader();
//detect and adjust the output BOM to be used
if (Reader::BOM_UTF16_LE != $reader->getInputBOM()) {
$reader->setOutputBOM(Reader::BOM_UTF16_LE);
}
//let's add the corresponding BOM
$reader->output('toto le héros.csv');
14 changes: 14 additions & 0 deletions examples/bom_excel_win.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

error_reporting(-1);
ini_set('display_errors', '1');

use League\Csv\Reader;

require '../vendor/autoload.php';

$csv = Reader::createFromPath(__DIR__.'/data/prenoms.csv');
if (Reader::BOM_UTF8 != $reader->getInputBOM()) {
$reader->setOutputBOM(Reader::BOM_UTF8);
}
$csv->output('test.csv');

0 comments on commit 3d3e40a

Please sign in to comment.