简体   繁体   English

PHP换行符file_put_contents()

[英]PHP line break with file_put_contents()

I can't seem to figure out why the following code doesn't produce a new line in my text file - neither does using \\n etc either - any ideas what could be wrong? 我似乎无法弄清楚下面的代码为什么在我的文本文件中没有产生新行-也没有使用\\ n等-任何想法可能出问题了吗?

    $data = $name . ' | ' . $_POST['comment'] . PHP_EOL;

    //write to file
    $f = file_put_contents('posts.txt', $data, FILE_APPEND);

Are you viewing the text file in an internet browser by any chance? 您是否有机会在Internet浏览器中查看文本文件?

If you are, the browser will get rid of the newline characters (unless you're using PRE tags). 如果是这样,浏览器将摆脱换行符(除非您使用的是PRE标签)。

Try double quotes: $data = $name . ' | ' . $_POST['comment'] . "\\n"; 尝试双引号: $data = $name . ' | ' . $_POST['comment'] . "\\n"; $data = $name . ' | ' . $_POST['comment'] . "\\n";
Or: $data = "$name | {$_POST['comment']}\\n"; 或: $data = "$name | {$_POST['comment']}\\n";

Have you tried \\r or \\n\\r ? 您尝试过\\ r或\\ n \\ r吗? Just an idea. 只是一个主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM