Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何读取包含空格和换行符的txt文件_Php_File Get Contents - Fatal编程技术网

Php 如何读取包含空格和换行符的txt文件

Php 如何读取包含空格和换行符的txt文件,php,file-get-contents,Php,File Get Contents,我有一个flatfiledatabase-001.txt文件,其中包含条目。我的txt文件如下所示: Bill message Bill goes here 1571436403 == John message John goes here 1571436126 == Earl message earl goes here 1571436051 == 为了读取.txt文件,我使用以下代码: $flatfile = file_get_contents('database-001

我有一个flatfiledatabase-001.txt文件,其中包含条目。我的txt文件如下所示:

Bill

message Bill goes here
1571436403

==
John

message John goes here
1571436126

==
Earl

message earl goes here 
1571436051

==


为了读取.txt文件,我使用以下代码:

$flatfile = file_get_contents('database-001.txt');
echo $flatfile;
?>
但是像这样使用它,它会产生这样的结果:

Bill message Bill goes here 1571436403==Johnmessage John goes here1571436126==Earlmessage earl goeshere 1571436051==
我如何才能像在.txt文件中一样读取包含换行符和白线的内容?

尝试:

$flatfile = file_get_contents('database-001.txt');
echo nl2br($flatfile);
发生的情况是,浏览器无法理解
\n
。您应该改用

nl2br
将所有经典新行
\n
转换为

您可以通过以下示例轻松看到这一点:

$str1 = "regular \n newline";
$str2 = "browser <br> newline";
echo str1;
echo str2;
$str1=“常规\n换行符”;
$str2=“浏览器
换行符”; echo-str1; 回波str2;
您是否在浏览器中查看结果?是的,我在浏览器中查看结果,而不是在
标记中显示结果。@kaczmen。这是有道理的。非常感谢。