]> granicus.if.org Git - php/commitdiff
Fixed bug #22382 (fgetcsv did not handle \" correctly).
authorIlia Alshanetsky <iliaa@php.net>
Mon, 24 Feb 2003 03:13:25 +0000 (03:13 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Mon, 24 Feb 2003 03:13:25 +0000 (03:13 +0000)
ext/standard/file.c
ext/standard/tests/file/bug22382.phpt [new file with mode: 0644]
ext/standard/tests/file/test2.csv [new file with mode: 0644]

index fa7649b45c61504401a30f60bf061fe61ad26b73..810367f69494f01fbfb7447c6add75f7141ef8f4 100644 (file)
@@ -2558,6 +2558,21 @@ PHP_FUNCTION(fgetcsv)
 
                        /* 2A. handle enclosure delimited field */
                        while (*bptr) {
+                               /* we need to determine if the enclosure is 'real' or is it escaped */
+                               if (*(bptr - 1) == '\\') {
+                                       int escape_cnt = 0;
+                                       char *bptr_p = bptr - 2;
+                               
+                                       while (bptr_p > buf && *bptr_p == '\\') {
+                                               escape_cnt++;
+                                               bptr_p--;
+                                       }
+                                       if (!(escape_cnt % 2)) {
+                                               goto normal_char;
+                                               continue;
+                                       }
+                               }
+                       
                                if (*bptr == enclosure) {
                                        /* handle the enclosure */
                                        if ( *(bptr+1) == enclosure) {
@@ -2571,6 +2586,7 @@ PHP_FUNCTION(fgetcsv)
                                                break;  /* .. from handling this field - resumes at 3. */
                                        }
                                } else {
+normal_char:
                                /* normal character */
                                        *tptr++ = *bptr++;
 
diff --git a/ext/standard/tests/file/bug22382.phpt b/ext/standard/tests/file/bug22382.phpt
new file mode 100644 (file)
index 0000000..5255eaf
--- /dev/null
@@ -0,0 +1,27 @@
+--TEST--
+Bug #22382: fgetcvs does not handle escaped quotes correctly
+--POST--
+--GET--
+--FILE--
+<?php
+$fp = fopen(dirname(__FILE__)."/test2.csv", "r");
+while(($line = fgetcsv($fp, 1024))) {
+       var_dump($line);
+}
+fclose($fp);
+?>
+--EXPECT--
+array(6) {
+  [0]=>
+  string(3) "One"
+  [1]=>
+  string(7) "\"Two\""
+  [2]=>
+  string(7) "Three\""
+  [3]=>
+  string(4) "Four"
+  [4]=>
+  string(2) "\\"
+  [5]=>
+  string(28) "\\\\\\\\\\\\\\\\\\\\\\\"\\\\"
+}
\ No newline at end of file
diff --git a/ext/standard/tests/file/test2.csv b/ext/standard/tests/file/test2.csv
new file mode 100644 (file)
index 0000000..d816464
--- /dev/null
@@ -0,0 +1 @@
+"One","\"Two\"","Three\"","Four","\\","\\\\\\\\\\\\\\\\\\\\\\\"\\\\"