So I ran into an issue where a client sent me a sample file and the line breaks were not preserved when reading the file using PHP from linux. So I wrote up a little function that should hopefully fix the issue for all platforms.
This is from a larger File class. The function names are pretty self explanatory.
public function fixLineBreaks() {
$this->openFile();
// replace all placeholder with proper \r\n
$contents = str_replace("[%LINEBREAK%]", "\r\n",
// replace all double place holder with single
// in case both \r and \n was used.
str_replace("[%LINEBREAK%][%LINEBREAK%]", "[%LINEBREAK%]",
// replace all \n with place holder
str_replace("\n", "[%LINEBREAK%]",
// replace all \r with placeholder
str_replace("\r", "[%LINEBREAK%]",
// First, read the file.
fread($this->fp, filesize($this->filename))
)
)
)
);
$this->closeFile();
$this->writeOpen();
fwrite($this->fp, $contents);
$this->closeFile();
}
EDIT
Okay, to clarify. I received a CSV file that was supposed to be parsed in PHP. Open it up in notepad and everything is on one line. Opening it with fopen and then reading with fgetcsv would also return the contents as one line. However, if you copy and paste the contents from notepad into NetBeans then all the line breaks are there. This made me realize that there are line breaks but they're not the ones that notepad or PHP recognize.
This was the function that I wrote up which essentially re-writes the file with proper line breaks.
I was posting this here to see if anyone possible had a better solution.
So I ran into an issue where a client sent me a sample file and the line breaks were not preserved when reading the file using PHP from linux. So I wrote up a little function that should hopefully fix the issue for all platforms.
This is from a larger File class. The function names are pretty self explanatory.
public function fixLineBreaks() {
$this->openFile();
// replace all placeholder with proper \r\n
$contents = str_replace("[%LINEBREAK%]", "\r\n",
// replace all double place holder with single
// in case both \r and \n was used.
str_replace("[%LINEBREAK%][%LINEBREAK%]", "[%LINEBREAK%]",
// replace all \n with place holder
str_replace("\n", "[%LINEBREAK%]",
// replace all \r with placeholder
str_replace("\r", "[%LINEBREAK%]",
// First, read the file.
fread($this->fp, filesize($this->filename))
)
)
)
);
$this->closeFile();
$this->writeOpen();
fwrite($this->fp, $contents);
$this->closeFile();
}
So I ran into an issue where a client sent me a sample file and the line breaks were not preserved when reading the file using PHP from linux. So I wrote up a little function that should hopefully fix the issue for all platforms.
This is from a larger File class. The function names are pretty self explanatory.
public function fixLineBreaks() {
$this->openFile();
// replace all placeholder with proper \r\n
$contents = str_replace("[%LINEBREAK%]", "\r\n",
// replace all double place holder with single
// in case both \r and \n was used.
str_replace("[%LINEBREAK%][%LINEBREAK%]", "[%LINEBREAK%]",
// replace all \n with place holder
str_replace("\n", "[%LINEBREAK%]",
// replace all \r with placeholder
str_replace("\r", "[%LINEBREAK%]",
// First, read the file.
fread($this->fp, filesize($this->filename))
)
)
)
);
$this->closeFile();
$this->writeOpen();
fwrite($this->fp, $contents);
$this->closeFile();
}
EDIT
Okay, to clarify. I received a CSV file that was supposed to be parsed in PHP. Open it up in notepad and everything is on one line. Opening it with fopen and then reading with fgetcsv would also return the contents as one line. However, if you copy and paste the contents from notepad into NetBeans then all the line breaks are there. This made me realize that there are line breaks but they're not the ones that notepad or PHP recognize.
This was the function that I wrote up which essentially re-writes the file with proper line breaks.
I was posting this here to see if anyone possible had a better solution.
Fix line breaks, cross-platform
So I ran into an issue where a client sent me a sample file and the line breaks were not preserved when reading the file using PHP from linux. So I wrote up a little function that should hopefully fix the issue for all platforms.
This is from a larger File class. The function names are pretty self explanatory.
public function fixLineBreaks() {
$this->openFile();
// replace all placeholder with proper \r\n
$contents = str_replace("[%LINEBREAK%]", "\r\n",
// replace all double place holder with single
// in case both \r and \n was used.
str_replace("[%LINEBREAK%][%LINEBREAK%]", "[%LINEBREAK%]",
// replace all \n with place holder
str_replace("\n", "[%LINEBREAK%]",
// replace all \r with placeholder
str_replace("\r", "[%LINEBREAK%]",
// First, read the file.
fread($this->fp, filesize($this->filename))
)
)
)
);
$this->closeFile();
$this->writeOpen();
fwrite($this->fp, $contents);
$this->closeFile();
}