0

Hey guys im trying to save the values into an textfile. I tried different ways and always when i open the textfile all the values are written in one line without space. I want to write each value in its own line here is what i have so far:

    $filename = 'admin_liste.txt';
    $string   = '';

    foreach($arr as $key => $val) {
    $string .= "$val\n";
    }

    file_put_contents($filename, $string); 

This writes everything like this in the textfile: user1user2user3user4

But i want it like this:

  • user1
  • user2
  • user3
  • user4

Without the "●"

2
  • @Ifthikhan yes im working on windows Commented Nov 19, 2012 at 9:40
  • try using \n\r instead of \n alone Commented Nov 19, 2012 at 9:42

4 Answers 4

4

You are using the Linux newline character, you need to use the \r\n for windows.

So your code will look like this:

$filename = 'admin_liste.txt';
$string   = '';

foreach($arr as $key => $val) {
    $string .= "$val\r\n";
}

file_put_contents($filename, $string);

However, PHP has a built in constant which will handle this dependant on the operating system you are running. So instead of using the above, you could simply use the following (no point if your website is always going to be on Windows though, but point to know):

file_put_contents($filename, implode(PHP_EOL,array_values($arr)));
Sign up to request clarification or add additional context in comments.

5 Comments

@Pgr456 then there is something else that we are not aware of. Can yoy supply any other info?
@Pgr456 Are you viewing the text file in a browser? If so, you need to, either check the source code, or call header( 'Content-Type: text/plain' ); before outputting the file to a browser, because PHP output is typically rendered as text/html in browsers. And HTML rendering will render \n\r as a single horizontal space.
@fireeyedboy +1 Very good point! However, you wont be able to send a header with the file as it is a plain text file, not a PHP file. Just open the file using a text editor, not a browser.
@BenCarey +1 Ugh, you're right of course. I was kind of thinking he perhaps might be calling something like readfile() to output the file in a browser. But that might be a bit unlikely, the more I think about it. But you never know. ;-)
@Pgr456 Have the above amendments sorted this?
3

Since you are working on windows you should use '\r\n'. I suggest that you use the built-in constant PHP_EOL as it would render the newline character based on the platform your are currently running.

EDIT

Added the PHP_EOL to your snippet and seems to work.

$filename = 'admin_liste.txt';
$string   = '';
$arr = array('user1', 'user2', 'user3');

foreach($arr as $key => $val) {

    $string .= "$val" . PHP_EOL;

}

file_put_contents($filename, $string);

1 Comment

Edited your snippet with PHP_EOL constant and it works for me.
0

You can simple use PHP_EOL constant

file_put_contents($filename, implode(PHP_EOL,array_values($arr)));

Comments

0

Try this code

 $filename = 'admin_liste.txt';
$string   = '';

foreach($arr as $key => $val) {
$string .= "$val\n\r";

}
file_put_contents($filename, $string); 

3 Comments

What difference will concatenating make!?
you need to answer not a type
OK, you corrected your suggestion, thanks! Though, seriously, that was not a typing error :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.