Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP file_put_contents new line issue

Tags:

php

I'm unable to write array to text file in new lines. My code:

echo '<pre>';
print_r($final_result);
echo '</pre>';

Output:

Array

(
    [0] => Something1
    [1] => Something2
    [2] => Something3
)

Then:

file_put_contents($file, trim($final_result . "\n"), FILE_APPEND);

Output:

Something1Something2Something3Array

My goal is:

Something1
Something2
Something3

Any ideas? :)

like image 300
Kris Avatar asked Sep 25 '13 23:09

Kris


People also ask

How to add new line in text file using PHP?

Answer: Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br() function which inserts HTML line breaks before all newlines in a string.

What is file_ put_ contents in PHP?

The file_put_contents() function in PHP is an inbuilt function which is used to write a string to a file. The file_put_contents() function checks for the file in which the user wants to write and if the file doesn't exist, it creates a new file.


1 Answers

How about

file_put_contents($file, implode(PHP_EOL, $final_result), FILE_APPEND);
like image 173
Phil Avatar answered Oct 05 '22 12:10

Phil