|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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""
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 19:00:01 2025 UTC |
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).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