PHP fgetcsv() delimiter

Mohammad Saberi picture Mohammad Saberi · Aug 22, 2012 · Viewed 11.5k times · Source

Regarding to fgetcsv() documentation, there are some parameteres inside fgetcsv() function: Handle, length, delimiter, enclosure, and escape.

Now, I have 2 questions:

  1. What does enclosure do exactly?
  2. I have a csv file with 5 columns per line. Imagine it something like this: 1,2,3,4,5

So indexes are from 0 to 4. But whenever I want to get date from index 4, an empty value returns. Unless I put a comma after it (by filling an extra column after it that makes the contents like this: 1,2,3,4,5,6 ). How can I solve this issue ? It seems that there is some problem because of missing comma after the last item in each row of csv file!

Answer

newfurniturey picture newfurniturey · Aug 22, 2012

 1. The enclosure parameter is the character the encapsulates the data for a specific field, or "index". By default, the parameter is a ", which means that you can "enclose" strings in " characters.

Example:

1,2,"this is three",4


 2. Per a comment, you're calling fgetcsv($handle, 10000, ','). It's possible, maybe, that the line(s) you're reading are longer than 10000 characters. Try changing the length to 0, which will be "no limit" and see if that helps. Another solution would be to try wrapping the column's value in double-quotes.