【问题标题】:echo text file with multiple lines in php [closed]在php中回显具有多行的文本文件[关闭]
【发布时间】:2020-12-08 06:20:41
【问题描述】:

我想在我的 php 代码中打印一个文本文件,但它给我的输出没有多行! 认为我的 txt 文件是这样的: (name = doc.txt)

hello guys!
we are here
Lemon is with You!

现在我的代码是:

<?php
$myfile = fopen("doc.txt", "r") or die("Unable to open file!");
echo fread($myfile, 100);
fclose($myfile);
?>

现在我的问题是我的输出:

大家好!我们在这里 柠檬与你同在!

我想要多行!怎么样?

【问题讨论】:

  • 它已经是那种格式了。由于您在浏览器中查看,\n 对它没有意义。使用命令行查看。

标签: php file echo fread


【解决方案1】:

一种解决方案是将&lt;pre&gt; 标签添加到您的输出中:

echo "<pre>" . fread($myfile, 100) . "</pre>;

【讨论】:

    【解决方案2】:

    因为它无法获取文件中是否有新行,所以你需要使用\n来产生新行 在 doc.txt 文件中

    hello guys! \n
    we are here \n
    Lemon is with You!
    

    然后

    <?php
    $myfile = fopen("doc.txt", "r") or die("Unable to open file!");
    echo fread($myfile, 100);
    fclose($myfile);
    ?>
    

    【讨论】:

    • 那无济于事。输出到浏览器,浏览器将忽略换行符,除非您使用换行符标记它,例如&lt;pre&gt;&lt;div style="white-space: pre"&gt;
    • 考虑用户用 Enter 写它
    猜你喜欢
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 2023-03-28
    • 2013-09-30
    • 2021-09-27
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多