Closed
Description
I have the following code:
$csv = '"Paret name","Child name","Title"
"parent 1","child 1","title"
"parent 2","child 2","title"
';
$csvReader = Reader::createFromString($csv);
$assoc = $csvReader->fetchAssoc();
And the associative array returned by fetchAssoc
looks like this:
Array (
[0] => Array (
["Paret name"] => parent 1
[Child name] => child 1
[Title] => title
)
[1] => Array (
["Paret name"] => parent 2
[Child name] => child 2
[Title] => title
)
)
Where parent name
contains the quotes "
. At fist, I thought there was something wrong with the fetchAssoc
method but after close inspection I realized that the file I was getting the CSV from was UTF-8
with BOM. Then I try to set stripBom
to true
, but it didn't work as I was still getting the same output.
$csv = '"Paret name","Child name","Title"
"parent 1","child 1","title"
"parent 2","child 2","title"
';
$csvReader = Reader::createFromString($csv);
$csvReader->stripBom(true);
$assoc = $csvReader->fetchAssoc();
The only way for me to get it to work was to manually strip the BOM before creating the reader:
$csv = preg_replace('/\x{FEFF}/u', '', $csv);
- Am I using the reader correctly?
- Should I expect the reader to automatically remove the BOM for me if
stripBom
is set to true?