1

I am trying to get my text file to style properly when I output it via php.

my code in index.html is:

<?php include 'read.php'?>  

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

In read.php I have:

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

when I look at this with google DevTools, I find that my text, even though inside the span/div element, it does not inherit the properties, so just outputs in a straight line, rather than with new lines like I want it (how it is in the text file, and how it shows up in DevTools). see picture below. In DevTools, this is how I want this to output in the index.html.

enter image description here

outputs as: 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 all on one line (see below)

enter image description here

Amit Sharma
  • 1,775
  • 3
  • 11
  • 20
user280339
  • 69
  • 1
  • 8

1 Answers1

1

Your browser expects HTML tags while file includes only NEWLINE symbols.

Change:

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

to

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

This will change all newline symbols to <br> tags.

Another option is to wrap the contents of the file in <pre> tags.

Bartosz Pachołek
  • 1,278
  • 1
  • 8
  • 17