Jump to content

Recommended Posts

My .csv file has 3 columns and 10 rows
However, the following only returns one row:

$file = fopen('document.csv', 'r');
while (($data = fgetcsv($file)) !== FALSE) {
$string .= $data[0].','.$data[1].','.$data[2]."\n";
}
print $string;

If I open the original .csv file in a text editor and replace all the invisible end-of-line "\r" with "\n" then the above code works fine.

There must be an efficient way to implement this in my script, without having to remember to modify the original .csv file first....

Thank you in advance 😀

Link to comment
https://forums.phpfreaks.com/topic/310556-fgetcsv-end-of-line/
Share on other sites

Your while statement is incorrect.  The FALSE is not in the while statement.  Drop it - you don't need it.

As for the input data, perhaps you should research how to create the csv file correctly instead of trying to fix it afterwards?

One question.   If you have a csv file why do you need to run this script just to create another one?  

Edited by ginerjm
Link to comment
https://forums.phpfreaks.com/topic/310556-fgetcsv-end-of-line/#findComment-1576752
Share on other sites

from the fgetcsv documentation - 

Quote

Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.


 

Link to comment
https://forums.phpfreaks.com/topic/310556-fgetcsv-end-of-line/#findComment-1576753
Share on other sites

Ginerjm, thank you, but I am provided with the .csv file, I don't create it myself.
Also, what is wrong with the "!==false" ?? I got it from the examples in the manual. Are the examples wrong? Rather, are you saying the "!==false" is not necessary, and that "while ($data = fgetcsv($file)" is enough? I'll certainly try that, I'm all for efficiency. At my level though, it's never wise to monkey with what's prescribed in the manual...

Mac_Gyver, ya, it was probably created on a Mac....

I guess if there are no elegant solutions (e.g. a hypothetical EOL argument to add to fgetcsv), then I'll just keep doing what I've been doing: put 3 lines of code at the top of my script (open file, replace all "\r" with "\n", put file back), and then run my script on the repaired file...

Thank you all, again, for your replys.

Link to comment
https://forums.phpfreaks.com/topic/310556-fgetcsv-end-of-line/#findComment-1576759
Share on other sites

Mac_gyver, I've implemented the auto_detect_line_endings config, and I'm not noticing any slowdown, so I'll use that from now on. The string replace "\r" with "\n" was working fast (even on big files), but then I'm assuming the line is ending with "\r". So auto_detect it is! Thank you!!

Link to comment
https://forums.phpfreaks.com/topic/310556-fgetcsv-end-of-line/#findComment-1576761
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.