【问题标题】:how to read a txt file with spaces and line breaks into it [duplicate]如何读取带有空格和换行符的txt文件[重复]
【发布时间】:2019-10-29 12:17:04
【问题描述】:

我有一个平面文件 database-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 文件中一样读取带有换行符和白线的内容?

【问题讨论】:

  • 您是在浏览器中查看结果吗?
  • 是的,我在浏览器中查看结果
  • 比在<pre>标签中显示。
  • @kaczmen。这是有道理的。谢谢

标签: php file-get-contents


【解决方案1】:

试试:

$flatfile = file_get_contents('database-001.txt');
echo nl2br($flatfile);

发生的事情是,浏览器不理解\n。您应该改用<br>nl2br 将所有经典的新行 \n 转换为 <br>

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

$str1 = "regular \n newline";
$str2 = "browser <br> newline";
echo str1;
echo str2;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    相关资源
    最近更新 更多