From:             b_ulrich at t-online dot de
Operating system: Linux
PHP version:      4.3.9
PHP Bug Type:     Filesystem function related
Bug description:  Wrong behavior in fgetcsv if field contains newlines AND \ 
followed by "

Description:
------------
If a csv field contains something like ...\"...\"... followed by a
newline, fgetcsv assumes that the newline is the end of the datarow even
it isn't the end of the field.

There's no problem if the field only contains text and newlines or only
...\"...\"...

If you create a Spreadsheet (2Rows with 3Columns) containing this:

<Row 1 Column 1>: R1C1
<Row 1 Column 2>: say \"YES\" to
element in next line
<Row 1 Column 3>: R1C3
<Row 2 Column 1>: R2C1
<Row 2 Column 2>: something with
multilines
<Row 2 Column 3>: R2C3

and export this to CSV, you will get:

R1C1;"say \""YES\"" to
element in next line";R1C3
R2C1;"something with
multilines";R2C3

This is correct Standard-CSV. But if you try to read it with fgetcsv you
will get a Result containing 3 Rows, the first and the second containing 2
Elements, the third contains 3 Elements.

Reproduce code:
---------------
<?php

$csv_data="R1C1;\"say \\\"\"YES\\\"\" to\nelement in next
line\";R1C3\nR2C1;\"something with\nmultilines\";R2C3\n";

$filename = "/tmp/test.csv";
$handle = fopen($filename, "wb");
fwrite($handle, $csv_data,strlen($csv_data));
fclose($handle);

$handle = fopen($filename, "rb");
while (($data = fgetcsv($handle,100000,";","\"")) !== FALSE) {
  $result[]=$data;
} // 
fclose($handle);

print_r($result);

?>


Expected result:
----------------
Array
(
    [0] => Array
        (
            [0] => R1C1
            [1] => say \"YES\" to
element in next line
            [2] => R1C3
        )

    [1] => Array
        (
            [0] => R2C1
            [1] => something with
multilines
            [2] => R2C3
        )

)


Actual result:
--------------
Array
(
    [0] => Array
        (
            [0] => R1C1
            [1] => say \"YES\" to
        )

    [1] => Array
        (
            [0] => element in next line"
            [1] => R1C3
        )

    [2] => Array
        (
            [0] => R2C1
            [1] => something with
multilines
            [2] => R2C3
        )

)


Note: There's no Problem, if there is no linebreak in Row1 Column2 and
there is also no Problem if you don't have the backslashes in the line.


-- 
Edit bug report at http://bugs.php.net/?id=31074&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=31074&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=31074&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=31074&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=31074&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=31074&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=31074&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=31074&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=31074&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=31074&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=31074&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=31074&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=31074&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=31074&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=31074&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=31074&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=31074&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=31074&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=31074&r=float
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=31074&r=mysqlcfg

Reply via email to