Skip to content

Instantly share code, notes, and snippets.

@nyamsprod
Last active August 29, 2015 13:58
Show Gist options
  • Save nyamsprod/9932158 to your computer and use it in GitHub Desktop.
Save nyamsprod/9932158 to your computer and use it in GitHub Desktop.
Using League\Csv with stream filter
<?php
stream_filter_register("utf8encode", "Utf8EncodeFilter");
class Utf8EncodeFilter extends php_user_filter
{
function filter($in, $out, &$consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable($in)) {
$bucket->data = utf8_encode($bucket->data);
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
}
$path = '/path/to/my/iso-8859-15-file.csv';
$stream_filter_path = 'php://filter/read=utf8encode/resource='.$path;
$csv = new \League\Csv\Reader($stream_filter_path);
//the CSV is directly transcoded in UTF-8 you don't need to use $csv->setEncoding anymore !!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment