I need to work with csv files. I want to write an web app which is working with csv files and is written in php. Problem is all the csv files have diffrent line feeds /r, /n,/r/n/. I need to have the same in all of them. Till now i do it like this:
exec("sed -i 's/\r$//' files/uploads/$fileName"); // DOS to Unix
exec("sed -i 's/$/\r/' files/uploads/$fileName"); // Unix to DOS
And in the end all of them have /r/n. But what if i get a csv file with /r as line feed, how to deal with that?
Thanks for your help
You have a number of options right in php. For example:
$f=file_get_contents("files/uploads/$fileName");
$f=str_replace("\r","",$f);
file_put_contents("files/uploads/$filename",$f);
This will strip all RETURN characters ("\r") from the input and write the file back with just linefeeds ("\n").
I have yet to see a CSV/Spreadsheet file with just "\r" characters, but supposing that is the case you could also do: str_replace("\r","\n") and then reduce "\n\n" to a single "\n" (again using str_replace).
There is also a REGEX-based solution, but the regex-part of my brain is a little fried this morning.