Reputation: 15
I use the following command to display text files in the browser:
echo file_get_contents($filename);
But this command displays the lines of the message continuously. I want to display the lines of the article, such as a separate text file. What is your solution?
Upvotes: 1
Views: 433
Reputation: 625
only using file_get_contents will correctly output your file as text in your browser, the issue is that your browser will interpret it as html. In html multiple whitespaces are "merged" so your file is displayed as continous list.
You have to possibilities here :
header('Content-Type:text/plain');
echo file_get_contents($filename);
$text = file_get_contents($filename);
echo "<pre>".htmlEntities($text)."</pre>";
Upvotes: 0
Reputation: 26844
You can use nl2br
to insert HTML line breaks before all newlines in a string.
nl2br ( string $string [, bool $is_xhtml = TRUE ] ) : string
Returns string with
<br />
or<br>
inserted before all newlines (\r\n
,\n\r
,\n
and\r
).
Usage:
$content = file_get_contents($filename);
echo nl2br($content);
Upvotes: 4