file_get_contents写入文件时换行

来源:互联网 发布:如何获取车流量数据 编辑:程序博客网 时间:2024/06/02 06:02

今天在PHP中使用file_get_contents()函数将文本内容写入txt文件时,发现无法对内容进行换行,原先我是像这样写的:

<?php    file_put_contents('1.txt', 'hello\r\nworld');//Windows下

结果1.txt文件中的内容是:hello\r\nworld

刚开始也挺疑惑,后来才知道,PHP中单引号是无法解析\r\n换行符的,要换成双引号才行,如下:

<?php    file_put_contents('1.txt', "hello\r\nworld");

后来在网上查资料,发现PHP中有一个常量PHP_EOL可以适应不同操作系统中的文本换行,如:

<?php    file_put_contents('1.txt', "hello" . PHP_EOL . "world");


0 0
原创粉丝点击