Output text file with line breaks in PHP

One line of code:

 echo nl2br( file_get_contents('file.txt') );

If you just want to show the output of the file within the HTML code formatted the same way it is in the text file you can wrap your echo statement with a pair of pre tags:

echo "<pre>" . $pageText . "</pre>;

Some of the other answers look promising depending on what you are trying todo.


To convert the plain text line breaks to html line breaks, try this:

    $fh = fopen("filename.txt", 'r');

    $pageText = fread($fh, 25000);

    echo nl2br($pageText);

Note the nl2br function wrapping the text.