【问题标题】:file_get_contents text won't style correctly [duplicate]file_get_contents 文本的样式不正确[重复]
【发布时间】:2020-01-13 03:49:12
【问题描述】:

我试图让我的文本文件在通过 php 输出时正确设置样式。

我在 index.html 中的代码是:

<?php include 'read.php'?>  

 <style>
    #text{
      height:80%;
     }
 </style>

在 read.php 我有:

<?php
 $txt = file_get_contents( "2020-1-12 19:36:35.txt" );
 echo "<span style='height:80%;'>".$txt. "</span>";
?>

当我用 google DevTools 看这个时,我发现我的文本,即使在 span/div 元素内,它也不会继承属性,所以只是以直线输出,而不是像我想要的那样使用新行它(它在文本文件中的样子,以及它在 DevTools 中的显示方式)。见下图。在 DevTools 中,这就是我希望它在 index.html 中输出的方式。

输出为:Student has 1 minutes left on their account. (Tutor) [00:00:06] : asdf a (Tutor) [00:00:06] : sd fa (Tutor) [00:00:06] : sdfasd (Tutor) [00:00:07] : f ew (Tutor) [00:00:07] : rfew f (Tutor) [00:00:07] : as e 全部在一行(见下文)

【问题讨论】:

    标签: javascript php html css


    【解决方案1】:

    您的浏览器需要 HTML 标记,而文件仅包含 NEWLINE 符号。

    变化:

    $txt = file_get_contents( "2020-1-12 19:36:35.txt" );
    

    $txt = nl2br(file_get_contents( "2020-1-12 19:36:35.txt" ));
    

    这会将所有换行符更改为&lt;br&gt; 标签。

    另一种选择是将文件的内容包装在&lt;pre&gt; 标记中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-24
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多