I’m trying to open a text file and output its contents with the code below. The text file includes line breaks but when I echo the file its unformatted. How do I fix this?
Thanks.
<html> <head> </head> <body> $fh = fopen("filename.txt", 'r'); $pageText = fread($fh, 25000); echo $pageText; </body> </html>
Advertisement
Answer
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.