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

CSV Delimiter Counting not respecting quoted strings #305

Closed
zmears opened this issue Dec 27, 2017 · 0 comments
Closed

CSV Delimiter Counting not respecting quoted strings #305

zmears opened this issue Dec 27, 2017 · 0 comments

Comments

@zmears
Copy link

zmears commented Dec 27, 2017

This is:

- [X ] a bug report

What is the expected behavior?

When counting delimiters the CSV Reader class should respect enclosed strings and only count potential delimiters outside of enclosed strings.

What is the current behavior?

The csv reader counts any appearance of a delimiter.

What are the steps to reproduce?

Parse this csv:

"Date/Time","task","username"
"12/17/2017 14:35","viewpage","username1",
"12/17/2017 04:00","editpage","username5",
"11/29/2017 08:20","deletepage","username2",,

Output:

array (size=4)
  0 => 
    array (size=1)
      0 => string 'Date/Time,"task","username"' (length=27)
  1 => 
    array (size=1)
      0 => string '12/17/2017 14:35,"viewpage","username1",' (length=40)
  2 => 
    array (size=1)
      0 => string '12/17/2017 04:00,"editpage","username5",' (length=40)
  3 => 
    array (size=1)
      0 => string '11/29/2017 08:20,"deletepage","username2",,' (length=43)

Which versions of PhpSpreadsheet and PHP are affected?

dev/master - php 7.0

Potential Fix

Remove any quoted strings from the line before counting delimiters.

PhpOffice\PhpSpreadsheet\Reader

while (($line = fgets($this->fileHandle)) !== false && (++$numberLines < 1000)) {
            $countLine = [];
            for ($i = strlen($line) - 1; $i >= 0; --$i) {
...

to

while (($line = fgets($this->fileHandle)) !== false && (++$numberLines < 1000)) {
            $line = preg_replace('/('.$this->enclosure.'.*'.$this->enclosure.')/U', '', $line);
            $countLine = [];
            for ($i = strlen($line) - 1; $i >= 0; --$i) {
...

I am not sure if this will work with all possible enclosures, or if the enclosures are malformed.

PowerKiKi added a commit that referenced this issue Jan 28, 2018
- Support for PHP 7.2
- Support cell comments in HTML writer and reader - [#308](#308)
- Option to stop at a conditional styling, if it matches (only XLSX format) - [#292](#292)
- Support for line width for data series when rendering Xlsx - [#329](#329)

- Better auto-detection of CSV separators - [#305](#305)
- Support for shape style ending with `;` - [#304](#304)
- Freeze Panes takes wrong coordinates for XLSX - [#322](#322)
- `COLUMNS` and `ROWS` functions crashed in some cases - [#336](#336)
- Support XML file without styles - [#331](#331)
- Cell coordinates which are already a range cause an exception [#319](#319)
Dfred pushed a commit to Dfred/PhpSpreadsheet that referenced this issue Nov 20, 2018
Dfred pushed a commit to Dfred/PhpSpreadsheet that referenced this issue Nov 20, 2018
- Support for PHP 7.2
- Support cell comments in HTML writer and reader - [PHPOffice#308](PHPOffice#308)
- Option to stop at a conditional styling, if it matches (only XLSX format) - [PHPOffice#292](PHPOffice#292)
- Support for line width for data series when rendering Xlsx - [PHPOffice#329](PHPOffice#329)

- Better auto-detection of CSV separators - [PHPOffice#305](PHPOffice#305)
- Support for shape style ending with `;` - [PHPOffice#304](PHPOffice#304)
- Freeze Panes takes wrong coordinates for XLSX - [PHPOffice#322](PHPOffice#322)
- `COLUMNS` and `ROWS` functions crashed in some cases - [PHPOffice#336](PHPOffice#336)
- Support XML file without styles - [PHPOffice#331](PHPOffice#331)
- Cell coordinates which are already a range cause an exception [PHPOffice#319](PHPOffice#319)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant