StevenOliver Posted April 12, 2020 Share Posted April 12, 2020 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 😀 Quote Link to comment https://forums.phpfreaks.com/topic/310556-fgetcsv-end-of-line/ Share on other sites More sharing options...
ginerjm Posted April 12, 2020 Share Posted April 12, 2020 (edited) 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 April 12, 2020 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/310556-fgetcsv-end-of-line/#findComment-1576752 Share on other sites More sharing options...
mac_gyver Posted April 12, 2020 Share Posted April 12, 2020 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. Â Quote Link to comment https://forums.phpfreaks.com/topic/310556-fgetcsv-end-of-line/#findComment-1576753 Share on other sites More sharing options...
StevenOliver Posted April 13, 2020 Author Share Posted April 13, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310556-fgetcsv-end-of-line/#findComment-1576759 Share on other sites More sharing options...
StevenOliver Posted April 13, 2020 Author Share Posted April 13, 2020 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!! Quote Link to comment https://forums.phpfreaks.com/topic/310556-fgetcsv-end-of-line/#findComment-1576761 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.