0

I have searched the site but none of the answers i found has worked so far so i'm seeking additional help.

I want to print the lines from a text file line by line (one under the other) but i do not wish to use iframe here is my code so far:

HTML-PHP

<td>
<?php                                       
$handle = fopen("textfilelocation/$text.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
echo $line;
}                                       
fclose($handle);
} 
else {
echo "Error accessing Tracking Data";
} 
?>
</td>

Text file

Hello,World 
March 3, 2018, 3:00am
Enjoy, life

Ouput

Hello,World March 3, 2018, 3:00am Enjoy, life

What i want is

Hello,World 
March 3, 2018, 3:00am
Enjoy, life
Time
  • 3
  • 7

1 Answers1

2

If you're reading the file a line at a time because you think you need to do that to display each line on a new line in the browser, it isn't necessary. You just need to convert the line breaks to <br> tags.

<td>
    <?= nl2br(file_get_contents('example.txt')) ?>
</td>

Otherwise, if you are doing something else to $line before you output it, just append a <br>.

echo "$line<br>";
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • Awesome thank you very much that works perfectly! i upvoted you but its not showing, not sure how to build my rep yet – Time Mar 21 '18 at 18:02
  • @Time no worries. Re: rep, you might want to read this: https://meta.stackoverflow.com/questions/252149/how-does-a-new-user-get-started-on-stack-overflow – Don't Panic Mar 21 '18 at 19:11