From f41a18b1c08ec6fe494ec8ec354083a80df32046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Sun, 21 Jan 2018 17:00:20 +0100 Subject: [PATCH] Added support for DOS double line endings Fixed GitHub issue #75 --- parsecsv.lib.php | 2 +- tests/methods/SaveTest.php | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/parsecsv.lib.php b/parsecsv.lib.php index 790df4a..7c9012d 100644 --- a/parsecsv.lib.php +++ b/parsecsv.lib.php @@ -418,7 +418,7 @@ class parseCSV { $file = &$this->file; } - $mode = $append ? 'at' : 'wt'; + $mode = $append ? 'ab' : 'wb'; $is_php = preg_match('/\.php$/i', $file) ? true : false; return $this->_wfile($file, $this->unparse($data, $fields, $append, $is_php), $mode); diff --git a/tests/methods/SaveTest.php b/tests/methods/SaveTest.php index d871975..4780b87 100644 --- a/tests/methods/SaveTest.php +++ b/tests/methods/SaveTest.php @@ -32,6 +32,18 @@ class SaveTest extends PHPUnit\Framework\TestCase { $this->saveAndCompare($expected); } + public function testSaveWithDosLineEnding() { + $this->csv->linefeed = "\r\n"; + $expected = "SMS\r\n0444\r\n5555\r\n"; + $this->saveAndCompare($expected); + } + + public function testSaveWithUnixLineEnding() { + $this->csv->linefeed = "\n"; + $expected = "SMS\n0444\n5555\n"; + $this->saveAndCompare($expected); + } + public function testAllQuotes() { $this->csv->enclose_all = true; $expected = "\"SMS\"\r\"0444\"\r\"5555\"\r";