1

I'm working with a txt file and PHP and I need to save data into this txt file, save and open the file is not a problem with file_get_contents and file_put_contents but I have a doubt please help me if you can:

I present the info in this order:

issue info 1
issue info 2
issue info 3

as you can see I show to the user the issues in different lines, but that works when I display that info in the browser but if I open the txt file it shows this:

issue info 1issue info 2issue info3

all in the same line, how can I do in order to save the info into the txt file with the order:

issue info 1
issue info 2
issue info 3

thanks in avance, this is my code by the moment:

$filename = "C:/Users/usuario/Videos/Desktop/prueba.txt";
if(file_exists($filename)) {
$filestring = file_get_contents($filename, NULL, NULL);

$convert = explode("\n", $filestring);

for ($i=0;$i<count($convert);$i++)  
{
      echo $convert[$i]. "</br>";  
    }

    echo "</br>";
    $filestring.= "issue 1"."</br>";
    file_put_contents($filename, $filestring);
    echo "</br>";

}
else{
    die("ese file no existe");
}
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
Pablo Tobar
  • 614
  • 2
  • 13
  • 37

2 Answers2

1

Use \n in your code, see this example:

<?php

$string = '';

for($i = 1; $i <= 3; $i++) {
    $string .= 'issue info ' . $i . "\n";
}   

file_put_contents('11.txt', $string);

?>

Output:

issue info1
issue info2
issue info3
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
0

Why not just add a new line characters to your strings (before and after the BR tags)?

"\r\n"

This way, you'll get the line breaks in HTML and in text files.

Also, make sure to double-quote the newline characters, or else PHP will not evaluate them properly.

user229044
  • 232,980
  • 40
  • 330
  • 338
grill
  • 1,160
  • 1
  • 11
  • 24