Skip to content
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

Enclosure should be remove when stripping BOM sequence #102

Closed
onema opened this issue Jun 4, 2015 · 6 comments
Closed

Enclosure should be remove when stripping BOM sequence #102

onema opened this issue Jun 4, 2015 · 6 comments
Labels

Comments

@onema
Copy link

onema commented Jun 4, 2015

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?
@nyamsprod nyamsprod added the bug label Jun 4, 2015
@nyamsprod nyamsprod changed the title Strip BOM doens't seem to work properly Reader::fetchAssoc remove the BOM sequence too late Jun 4, 2015
@nyamsprod nyamsprod changed the title Reader::fetchAssoc remove the BOM sequence too late Enclosure should be remove when stripping BOM sequence Jun 4, 2015
nyamsprod added a commit that referenced this issue Jun 4, 2015
The enclosure character should also be stripped when
The BOM character is removed to properly output CSV content
@nyamsprod
Copy link
Member

@onema thanks for reporting the bug. A patch was made available on the master branch. After review and testing a patch release will be made next week. In the meantime you can test that the dev-master branch fix works for you

nyamsprod added a commit that referenced this issue Jun 4, 2015
Test regarding #102 issue added
@onema
Copy link
Author

onema commented Jun 4, 2015

Awesome! will update and be testing it today.

@onema
Copy link
Author

onema commented Jun 4, 2015

I just tested the master branch and fetch* methods work as long as I call stripBom(true) before each call. It threw me off a bit, but looking at the code it seems this is the intended behavior; I didn't see anything related to this in the docs. Is this the intended behavior?

$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();
$all = $csvReader->fetchAll();

$assoc

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
        )
)

$all

Array (
    [0] => Array (
            [0] => "Paret name"
            [1] => Child name
            [2] => Title
        )
    [1] => Array (
            [0] => parent 1
            [1] => child 1
            [2] => title
        )
    ...
)

@nyamsprod
Copy link
Member

Quoting the docs you are referring to:

After an extract/conversion method call, all query options are cleared;

This includes the stripBom method which is nothing more than a query option.

I know it may seems strange but this is a trade-off I choose when implementing the solution. Stripping BOM sequence is only useful when the first cell of the first row is expected in the result set. In all others situations you don't want to add another layer of Iterator when it is not needed.

@onema
Copy link
Author

onema commented Jun 5, 2015

👍 thank you for your work on this.

@nyamsprod
Copy link
Member

The patch release 7.1.2 is released with the bug fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants