New PHP line in fopen function

Asked

Viewed 102 times

0

Good people, I was following a PHP booklet and so far so good, but the problem is in writing the data in a notebook of windows, instead of it giving new line " n", he is putting the data behind each other as if not this enter, I will post the code and from now on thank you for your help...

$texto = $date."\t".$tireqty." Pneus \t".$oilqty." Oleo \t "
.$sparkqty." Plugues de velas \t $".$totalamount."\t".$address."\n";

if($arquivo = fopen("carteirainter.txt", 'ab'))
{
    fputs($arquivo, $texto."\n");
    fclose($arquivo);
}
else
    echo "Não foi possivel efetuar a gravaçao.<br>";

1 answer

1

Use the PHP_EOL constant:

fputs($arquivo, $texto.PHP_EOL);

Alternatively, you can force it like this:

fputs($arquivo, $texto."
");

Another way is to use the Carriage Return \r together with the newline \n:

fputs($arquivo, $texto."\r\n");

It is recommended that the environment is using a UTF8 character set.

Additional remark on the compatibility of Carriage Return and newline:

\n -> linux
\r -> Mac
\n -> Mac OS-X
\r\n -> Windows
  • pdf touch on the subject?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.