Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a string array to text file separated by new line character

Tags:

html

php

I have a PHP page which accepts input from user in a text area. Multiple strings are accepted as input from user & would contain '\n' and I am scanning it as:

$data = explode("\n", $_GET['TxtareaInput']);

Each string should be moved into the text file with new line character separation. This is the code I am using now and it separates each string with a '^M' character:

foreach($data as $value){
    fwrite($ourFileHandle, $value);
}

Is there anyway I can get each string followed by a carriage return?

like image 283
Klone Avatar asked Aug 14 '12 16:08

Klone


2 Answers

You can simply write it back using implode:

file_put_contents('file.csv', implode(PHP_EOL, $data));
like image 95
madflow Avatar answered Oct 18 '22 14:10

madflow


Try this:

$data = explode("\n", $_GET['TxtareaInput']);
foreach($data as $value){
    fwrite($ourFileHandle, $value.PHP_EOL);
}
like image 21
raidenace Avatar answered Oct 18 '22 14:10

raidenace