Jon
Jon

Reputation: 2584

Present Text file on webpage

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?

Upvotes: 0

Views: 103

Answers (2)

Robbert
Robbert

Reputation: 6592

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));

Upvotes: 3

Charles F
Charles F

Reputation: 114

HTML uses <br/> markup to make a line return, you should either do a foreach line and display a
markup, or use a pre markup to disregard formatting.

Upvotes: 0

Related Questions