How do I create line breaks in a .txt file that PHP can read?

LearningJunkie

New member
With PHP, I'm reading my .txt files using fopen() together with fgets(), but the resulting text on the web page does not show line breaks. It just starts the next line when it runs out of space at the edge of the window. In the text file I have tried creating line breaks using the return key, using \n, and using \r and have not found success with any of these. I've also tried saving as text document with line breaks and as a text document without line breaks. What am I missing?

I'm running a mac and using MAMP as my server.

Thank you!
Alright, so for reference, the text in the file is exactly as follows:
"If you're
reading this,
then you successfully
opened and read
the file.
Good job!
Now, is it apparent where one line ends
and another one starts?"

Taking your advice works. That is, using

$file = fopen("myfile.txt","r");

while(! feof($file))
{
echo fgets($file). "<br>";
}
fclose($file);

It does not, however, show the same line breaks as the original text file. Furthermore the following script ALSO shows the file in exactly the same way:

$file = fopen("myfile.txt", "r");

$text = fgets($file);
echo $text, "<BR>";

It's strange to me that with this script it doesn't stop after the first line break. It looks like the line breaks in my .txt file are just not being recognized. Do you know how we can make them recognized?

Thank you again!
In response to Chapaev:

Alright, so for reference, the text in the file is exactly as follows:
"If you're
reading this,
then you successfully
opened and read
the file.
Good job!
Now, is it apparent where one line ends
and another one starts?"

Taking your advice works. That is, using

$file = fopen("myfile.txt","r");

while(! feof($file))
{
echo fgets($file). "<br>";
}
fclose($file);

It does not, however, show the same line breaks as the original text file. Furthermore the following script ALSO shows the file in exactly the same way:

$file = fopen("myfile.txt", "r");

$text = fgets($file);
echo $text, "<BR>";

It's strange to me that with this script it doesn't stop after the first line break. It looks like the line breaks in my .txt file are just not being recognized. Do you know how we can make them recognized?

Thank you again!
 
So fgets() returns the whole file in one go? If not then you have to manually add line breaks after each line fgets() gets like so:

<?php
$file = fopen("test.txt","r");

while(! feof($file))
{
echo fgets($file). "<br>";
}

fclose($file);
?>
 
Line breaks in HTML (whether hand-written or generated by something like PHP) get condensed with other whitespace into a single space for each series of consecutive whitespace characters.

If you want to insert a newline into an HTML document, use the <br> tag. Or, better yet, enclose each paragraph in a <p> element.

If you don't want to include the HTML tags in the .txt file you're reading from, just replace them all with "<br>" after you've read them from the file, but before you output the text to the web page.
 
Back
Top