php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #38918 fputcsv() fgetcsv() inconsistency odd number trailing backslashes
Submitted: 2006-09-22 00:03 UTC Modified: 2006-11-16 20:14 UTC
Votes:4
Avg. Score:4.8 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:2 (50.0%)
From: mike at opendns dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5.2.0 OS: Linux, debian sarge
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mike at opendns dot com
New email:
PHP Version: OS:

 

 [2006-09-22 00:03 UTC] mike at opendns dot com
Description:
------------
If a string you write to a CSV file with fputcsv() ends with an _odd_ number of backslashes, then when you read it back in with fgetcsv(), it'll miss the delimeter and combine two elements into one.

Reproduce code:
---------------
$tmp_file = '/tmp/csv_f_up.tmp';

$data_to_write = array('string ends with _odd_ number of backslashes \\\\\\',"and isn't the last element");

echo "data_to_write:\n";
var_dump($data_to_write);

$h_w = fopen($tmp_file, 'w');
fputcsv($h_w, $data_to_write);
fclose($h_w);

$h_r = fopen($tmp_file, 'r');
$data_read_in = fgetcsv($h_r);
fclose($h_r);

echo "data_read_in:\n";
var_dump($data_read_in);

Expected result:
----------------
data_to_write:
array(2) {
  [0]=>
  string(48) "string ends with _odd_ number of backslashes \\\"
  [1]=>
  string(26) "and isn't the last element"
}
data_read_in:
array(2) {
  [0]=>
  string(48) "string ends with _odd_ number of backslashes \\\"
  [1]=>
  string(26) "and isn't the last element"
}
}

Actual result:
--------------
data_to_write:
array(2) {
  [0]=>
  string(48) "string ends with _odd_ number of backslashes \\\"
  [1]=>
  string(26) "and isn't the last element"
}
data_read_in:
array(1) {
  [0]=>
  string(77) "string ends with _odd_ number of backslashes \\\",and isn't the last element""
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-09-25 12:23 UTC] tony2001@php.net
This is expected since "\" is the escape character.
Reclassified as docu problem - fputcsv() does not escape the data, you have to do it yourself.
 [2006-10-15 04:10 UTC] dave@php.net
Are you sure this is a documentation problem? RFC 4180 says nothing about \ characters needing to be escaped, and quotes (") _are_ escaped properly in fputcsv(). The problem lies with fgetcsv() incorrectly parsing a single \ at the end of a value (two \'s work, which is inconsistent at best).
 [2006-11-16 20:14 UTC] mike at opendns dot com
this isn't a documentation problem.  after reading RFC 4180, it's clear this is a problem with fgetcsv().  also leading newlines cause problems.  will resubmit a new bug with better example code.
 [2010-08-03 10:26 UTC] surfchen at gmail dot com
yes, it is not documentation problem. so why not change the package attribute?

and also , confirm this with php 5.2.9 on windows:
$arr=array('\\','"');
$h=fopen('test.csv',"w");
fputcsv($h,$arr);
fclose($h);

$h=fopen('test.csv',"r");
$line=fgetcsv($h);
var_dump($line);

result:
array(1) {
  [0]=>
  string(6) "\",""
"
}


sorry for duplicate post, I missed the comments when reading #38929
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 08:01:29 2024 UTC