usertest
usertest

Reputation: 27628

Output text file with line breaks in PHP

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>

Upvotes: 21

Views: 61165

Answers (9)

Jeremy Heslop
Jeremy Heslop

Reputation: 558

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.

Upvotes: 11

Neurotransmitter
Neurotransmitter

Reputation: 6797

Say you have an index.php file hosted by the web server. You want to insert some multi-line text file contents into it. That's how you do it:

<body>
        <div>Some multi-line message below:</div> 
        <div><?= nl2br(file_get_contents('message.txt.asc')); ?></div>
</body>

This <?= ... ?> part is just a shorthand, which instructs the web server, that it needs to be treated as a PHP echo argument.

Upvotes: -1

chantal
chantal

Reputation: 1

Trying to get line breaks to work reading a .txt file on Apache2 and PHP 5.3.3 with MacOSX 10.6.6 and Camino, the echo nl2br( $text); didn't work right until I printed the file size first too. BTW it doesn't seem to matter if the .txt file has Linux/MacOSX LF or Windows CRLF line breaks or the text encoding is UTF-8 or Windows Latin1, Camino gets it out OK.

<?php
$filename     = "/Users/Shared/Copies/refrain.txt";
$file_ptr     = fopen ( $filename, "r" );
$file_size    = filesize ( $filename );
$text         = fread ( $file_ptr, $file_size );
fclose ( $file_ptr );
echo ( "File size : $file_size bytes<br>&nbsp;<br>" );
echo nl2br ( $text );
?>

Upvotes: 0

bng44270
bng44270

Reputation: 349

For simple reads like this, I'd do something like this:

$fileContent = file_get_contents("filename.txt");

echo str_replace("\n","&lt;br&gt;",$fileContent);

This will take care of carriage return and output the text. Unless I'm writing to a file, I don't use fopen and related functions.

Hope this helps.

Upvotes: 3

Gordon
Gordon

Reputation: 316969

One line of code:

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

Upvotes: 33

Jay Zeng
Jay Zeng

Reputation: 1421

You need to wrap your PHP code into <?php <YOU CODE HERE >?>, and save it as .php or .php5 (depends on your apache set up).

Upvotes: -1

MattBelanger
MattBelanger

Reputation: 5350

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.

Upvotes: 38

Robin Summerhill
Robin Summerhill

Reputation: 13675

Are you outputting to HTML or plain text? If HTML try adding a <br> at the end of each line. e.g.

while (!feof($handle)) {
  $buffer = fgets($handle, 4096); // Read a line.
  echo "$buffer<br/>";
} 

Upvotes: 0

brianary
brianary

Reputation: 9322

Before the echo, be sure to include

header('Content-Type: text/plain');

Upvotes: 2

Related Questions