php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20716 fgetcsv() cannot handle multi-line input
Submitted: 2002-11-29 04:52 UTC Modified: 2002-11-30 13:13 UTC
From: edink at proventum dot net Assigned:
Status: Closed Package: Filesystem function related
PHP Version: 4.3.0RC2 OS: RedHat Linux 6.2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: edink at proventum dot net
New email:
PHP Version: OS:

 

 [2002-11-29 04:52 UTC] edink at proventum dot net
fgetcsv() was able to read multi-line fields that were quoted. Here is the example of the test file:

f1;f2;f3
Test 1a;Test 1b;Test 1c
Test 2a;"Test 2b - line 1
Test 2b - line 2
Test 2b - line 3";Test 2c

We are trying to parse this file with the following code:

$f = @fopen($argv[1], "r");
if (!$f) exit("Cannot open input file.\n");

$v = array();

while ($data = fgetcsv ($f, 10000, ";"))
{
  $v[] = $data;
}

var_export($v);

Now, in PHP 4.2.3 and earlier this produces correct 3 element array. PHP 4.3.0-dev produces 5 element array because it gets confused with the multiple line input in the 2b field.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-29 05:00 UTC] edink at proventum dot net
The correct output (4.2.3):

array (
  0 =>
  array (
    0 => 'f1',
    1 => 'f2',
    2 => 'f3',
  ),
  1 =>
  array (
    0 => 'Test 1a',
    1 => 'Test 1b',
    2 => 'Test 1c',
  ),
  2 =>
  array (
    0 => 'Test 2a',
    1 => 'Test 2b - line 1
Test 2b - line 2
Test 2b - line 3',
    2 => 'Test 2c',
  ),
)

Incorrect output (4.3.0RC2):
array (
  0 =>
  array (
    0 => 'f1',
    1 => 'f2',
    2 => 'f3',
  ),
  1 =>
  array (
    0 => 'Test 1a',
    1 => 'Test 1b',
    2 => 'Test 1c',
  ),
  2 =>
  array (
    0 => 'Test 2a',
    1 => 'Test 2b - line 1',
  ),
  3 =>
  array (
    0 => 'Test 2b - line 2',
  ),
  4 =>
  array (
    0 => 'Test 2b - line 3"',
    1 => 'Test 2c',
  ),
)
 [2002-11-30 13:13 UTC] iliaa@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 13:01:30 2024 UTC