Home  >  Article  >  Backend Development  >  PHP向txt文件里面写入字符串,换行问题

PHP向txt文件里面写入字符串,换行问题

WBOY
WBOYOriginal
2016-06-23 13:57:35980browse

$DOCUMENTROOT = $_SERVER['DOCUMENT_ROOT'];    $file = fopen("$DOCUMENTROOT/php_02/123.txt","wb");    fwrite($file,"line 1 "."\n"." line 2 n line 3");    fclose($file);

这样没用
但是
$DOCUMENTROOT = $_SERVER['DOCUMENT_ROOT'];    $file = fopen("$DOCUMENTROOT/php_02/123.txt","wb");    fwrite($file,"line 1 "."\r\n"." line 2 n line 3");    fclose($file);

这样就可以了,问问为什么


回复讨论(解决方案)

是不同的编辑器解析效果不同

在命令行里面分别打开以下两个程序,然后用程序打开生成的文件,就可以看出程序是怎么解析\n的了
notepad
wordpad

Windows下很多编辑器把\r\n当换行,忽略\n

你把Word显示控制字符功能打开,\n显示为向下的箭头,\r\n显示为拐弯的箭头

window \r\n
linux \n

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn