Writing A String Array To Text File Separated By New Line Character
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('
Solution 1:
You can simply write it back using implode:
file_put_contents('file.csv', implode(PHP_EOL, $data));
Solution 2:
Try this:
$data = explode("\n", $_GET['TxtareaInput']);
foreach($dataas$value){
fwrite($ourFileHandle, $value.PHP_EOL);
}
Solution 3:
If you want to add new lines, then why are you first removing them?
$data = explode("\n", $_GET['TxtareaInput']);
Keep only this line:
fwrite($ourFileHandle, $data);
It will write your data to the file as it was received.
If you want to replace all new lines by carriage returns before writing to file, use this code:
fwrite($ourFileHandle, str_replace("\n", "\r", $data));
Post a Comment for "Writing A String Array To Text File Separated By New Line Character"