-
-
Notifications
You must be signed in to change notification settings - Fork 333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
May be a BOM issue? #184
Comments
@harikt there's nothing wrong with the library. By default the library thinks you are using a UTF-8 document. In your case the document is not UTF-8. Here's what I did: require 'vendor/autoload.php';
use League\Csv\Reader;
$file = __DIR__."/file.csv";
$reader = Reader::createFromPath($file);
$input_bom = $reader->getInputBOM();
//in your case $input_bom === Reader::BOM_UTF16_LE
//so I've just added this following line (I assume that the iconv extension is installed on your server)
$reader->appendStreamFilter('convert.iconv.UTF-16/UTF-8'); //converting to UTF-8
$reader->stripBOM(true);
echo "<pre>";
//get the first row, usually the CSV header
$headers = $reader->fetchOne(0);
$rows = $reader->setOffset(1)->fetchAssoc($headers); //Simpler than using the Reader::addFilter method
foreach ($rows as $row) {
echo json_encode($row, JSON_PRETTY_PRINT), PHP_EOL;
} |
@nyamsprod Thank you for your quick help. A few things I noticed are require 'vendor/autoload.php';
use League\Csv\Reader;
$file = __DIR__."/file.csv";
$reader = Reader::createFromPath($file);
$reader->appendStreamFilter('convert.iconv.UTF-16/UTF-8');
$reader->stripBOM(true);
$headers = $reader->fetchOne(0); Adding
Also closely looking the
Thank you |
@harikt strange I failed to reproduce this bug and I'm also using the 16.04 LTS <?php
error_reporting(-1);
ini_set('display_errors', '1');
require 'vendor/autoload.php';
use League\Csv\Reader;
$file = __DIR__."/test.csv";
$reader = Reader::createFromPath($file);
$reader->appendStreamFilter('convert.iconv.UTF-16/UTF-8');
$reader->stripBOM(true);
echo "<pre>";
foreach ($reader->fetchAssoc(0) as $row) {
echo json_encode($row, JSON_PRETTY_PRINT), PHP_EOL;
} |
I hope you could re-produce this now. This is when you call <?php
error_reporting(-1);
ini_set('display_errors', '1');
require 'vendor/autoload.php';
use League\Csv\Reader;
$file = __DIR__."/test.csv";
$reader = Reader::createFromPath($file);
$input_bom = $reader->getInputBOM();
if ( $input_bom === Reader::BOM_UTF16_LE || $input_bom === Reader::BOM_UTF16_BE ) {
echo "BOM UTF-16 " . PHP_EOL;
$reader->appendStreamFilter('convert.iconv.UTF-16/UTF-8');
}
$reader->stripBOM(true);
foreach ($reader->fetchAssoc(0) as $row) {
// echo json_encode($row, JSON_PRETTY_PRINT), PHP_EOL;
} |
@harikt thanks I found the issue ... I'll release a patch to fix this when I have time 👍 |
Good to hear you was able to reproduce the same. And thank you very much for the valuable support you have provided. I wasn't able to find the reason to send a PR. Sorry about that :( . |
Also in the case as I mentioned when calling |
I've looked into this issue and in fact again there's no issue. The thing is if you use stream filter you don't need to explicitly call the So the code should be: <?php
error_reporting(-1);
ini_set('display_errors', '1');
require 'vendor/autoload.php';
use League\Csv\Reader;
$file = __DIR__."/file.csv";
$reader = Reader::createFromPath($file);
$input_bom = $reader->getInputBOM();
if ($input_bom === Reader::BOM_UTF16_LE || $input_bom === Reader::BOM_UTF16_BE) {
$reader->appendStreamFilter('convert.iconv.UTF-16/UTF-8');
}
foreach ($reader->fetchAssoc(0) as $row) {
echo json_encode($row, JSON_PRETTY_PRINT), PHP_EOL;
} The I should document this into the library documentation.. or you could do it 👍 |
Thank you @nyamsprod . I will look into sending a PR regarding the same. Please give me some time. Thank you. |
version 8.1.2 is released with the fix |
Issue summary
Hi,
I have been playing with importing a csv file. It has came to my notice that there is some sort of issue with
μ
character. May be it is something else I am wrong.System informations
Standalone code, or other way to reproduce the problem
You can download the csv file from http://www.data-explorer.com/content/data/standard-model-of-particle-physics-csv.zip
Expected result
I am not sure what would be the appropriate character for
μ
.Actual result
There is actually more on the same csv which have similar issues.
Thank you.
The text was updated successfully, but these errors were encountered: