View Full Version : fgetcsv() splitting at enclosed delimiters

11-07-2007, 03:50 PM
I have not upgraded or changed PHP since I last used the function fgetcsv(), however it is now breaking up enclosed delimiters.

Take this for example:

12345,Yes,"Blah blah blah, yadda yadda yadda, something something something",50

When the following code is called it should produce 4 elements of data in an array but instead it now produces 6:

$fp = fopen($source_file, 'r');
$field_length = 4096;
$delimiter = ',';
$enclosure = '"';

$data = fgetcsv($fp, $field_length, $delimiter, $enclosure);

Why is this happening? Why would it suddenly begin happening without altering any code? Could a Windows update have caused this? That is the only thing that changed on the server. :confused:

11-07-2007, 04:07 PM
Are you sure the file still has double-quotes in it? Is the file still saved as an ANSI/ASCII file?

11-07-2007, 04:13 PM
Yes, it has the quotes. I just checked the file again and the answer to the problem immediately appeared. Inside the quote enclosed fields were doubled-double quotes. This is fine because it escapes the double quotes to a single double quote. Little did I realize but the first doubled-double quote has a back slash in front of it which was confusing the parser. Removing that fixed the issue.