Last active
August 29, 2015 13:58
-
-
Save nyamsprod/9932158 to your computer and use it in GitHub Desktop.
Using League\Csv with stream filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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