doushi3819244 2014-01-13 23:22
浏览 98
已采纳

在网页上显示文本文件

I have a text file "applications.txt" I want to show on the web using PHP, I have this line of code:

<?php
    $myfilename = "applications.txt";
    if(file_exists($myfilename)){
        echo file_get_contents($myfilename);
    }
?>

The contents of the text file looks like this:

Line one

Line two

Line three

However, on the webpage, it looks like this:

Line one Line two Line three

How do I make it display the new lines properly?

  • 写回答

2条回答 默认 最新

  • duanguanzai6181 2014-01-13 23:25
    关注

    This is happening because HTML ignores more than two concurrent whitespaces and treats them as a single one. You can wrap the output in <pre> tags

    echo "<pre>" . file_get_contents($myfilename) . "</pre>";
    

    that will preserve the file as it appears but can lead to problems for files with long lines. You can also replace newlines with <br/> tags using nl2br()

    echo nl2br(file_get_contents($myfilename));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM