Doc #38918 [Com]: fputcsv() fgetcsv() inconsistency odd number trailing backslashes

From: Date: Tue, 03 Aug 2010 08:26:59 +0000
Subject: Doc #38918 [Com]: fputcsv() fgetcsv() inconsistency odd number trailing backslashes
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-4783@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=38918&edit=1 ID: 38918 Comment by: surfchen at gmail dot com Reported by: mike at opendns dot com Summary: fputcsv() fgetcsv() inconsistency odd number trailing backslashes Status: Bogus Type: Documentation Problem Package: Documentation problem Operating System: Linux, debian sarge PHP Version: 5.2.0 Block user comment: N New Comment: 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 Previous Comments: ------------------------------------------------------------------------ [2006-11-16 20:14:03] 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. ------------------------------------------------------------------------ [2006-10-15 04:10:49] 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-09-25 12:23:21] 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-09-22 00:03:09] 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"" } ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=38918&edit=1

« previous php.doc.bugs (#4783) next »