I’m literally reading a file straight from the file system and outputting it directly to the browser, yet, the new lines are seemingly being turned into spaces.
If the new lines were being processed correctly, you would see breaks in the first image you posted, and multiple array elements in the second image you posted.
" Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. Upon failure, file() returns false ."
Unfortunately, I was unable to replicate the issue. PHP is able to break the files into array items properly.
I even copy pasted the data you provided and there’s also no issue reading it into an array.
I think maybe the file itself has some issues that might have prevented PHP from parsing it correctly (like a mix of LF and CRLF line endings, file being too big, etc).
I’m sorry but this is probably something that you’ll have to solve by yourself.
Alright. Not making sense to me. You have access to the exact file, and exact code that I used. You are saying there is a difference on the node you are running the code on? Not sure how I could possibly fix that, since it’s not my platform.
Not to be snark, but I plan to avoid the platform as much as possible and do data cleansing locally.
Man, this forum is terrible. @Frank419 executed both sets of code without issue. I posted the code being executed. @Frank419 is seemingly saying he is getting different results with the same data and same code. You have any other suggestions?
I had a look at the source file, and I think I see the issue (after some testing and sparring with ChatGPT).
The problem is that your file uses CR line endings, i.e. \r. That’s pretty unusual nowadays, and ChatGPT says that only very old MacOS used to do this.
Windows typically uses \r\n, and Unix/Linux just uses \n. But since there is no \n in your file, any tool I throw at it just says your file only has a single line.
I’m pretty sure that PHP does the line detection based on the standard line ending of the platform it runs on. Since our servers (like virtually all hosting servers), run on Linux, it expects lines are terminated with \n. And it’s not detecting \r as a valid line ending.
I don’t know where that file is from or what control you have over it. But here are a few things you can do:
See where the file came from, and see if you can get this file with more common line endings, either \n or \r\n.
Accept that PHP doesn’t detect the “line endings” in this file as line endings, and split the “lines” yourself. I.e. instead of using file to split the file for you, just read it with file_get_contents and use explode to separate the lines.
Were you able to get it to parse? If so, can you post the code? I switched to local Python for now, which seems to be working as expected for parsing the file.
Please make sure to put the separator in double quotes though. Escape sequences will only be interpreted as such when you use double quotes, with single quotes it’s just interpreted as the literal backslash character and letter. Like so: