Reputation: 12551
I am trying to get fgetcsv() to read a comma separated value file. Ordinarily this works as expected, but I've encountered a file that has some columns being split into new data lines instead of being treated as one.
Here is an example of a lines giving me trouble: pastie.org/5664800
The column that is creating the problem is the one with the multiple lines that make up the description.
fgetcsv ends the first read when it reaches the following line:
- Length/Width/Height: 3.75\""x2.4\""x2.4\"" (95.25X60.96x60.96)
So for instance, if I have the code:
ini_set('auto_detect_line_endings', true);
while ($row = fgetcsv($filepointer, 5000, ',', '"') {
echo '<pre>'; var_dump($row); echo '</pre><br />';
}
I get one mostly complete row of data that ends in "Length/Width/Height: 3.75\"x2.4\""x2.4\"" (95.25X60.96x60.96)
" and then every newline after that is treated as its own row all the way down to the end.
Any idea what's going on here?
Upvotes: 4
Views: 2130
Reputation: 50328
The problem is that your data contains the character sequence \""
. From the context, it's clear that this is supposed to represent the literal substring \"
; that is, the blackslash is supposed to be a literal backslash, while the double quote has been escaped by doubling it.
By default, however, fgetcsv()
treats the backslash as an escape character, so it parses the characters \"
as a backslash-escaped literal double quote, and then assumes that the second "
ends the double-quoted string.
The fix is simple, as long as you're using PHP 5.3.0 or later: just tell fgetcsv()
not to treat the backslash as an escape character by passing some other character as the fifth parameter to it. Apparently, passing null
or false
or ''
won't work to disable the escape character entirely, but passing '"'
(i.e. the same value as the quote character) does:
while ( $row = fgetcsv( $filepointer, 0, ',', '"', '"' ) ) {
var_export( $row );
echo "\n";
}
Upvotes: 6
Reputation: 212402
The fifth argument to fgetcsv() is how to escape the " enclosure character when it occurs within the enclosed string
Upvotes: 0