Page 1 of 1

Add a "Linebreak" in a .txt when there is a linebreak in txt

Posted: Tue Nov 08, 2011 1:01 pm
by Goofan
Hey,
Sorry for the long header.

My problem is as following.

I get the content of a .txt file with this code:

Code: Select all

$file = file_get_contents('./x.txt', true);
The .txt file got multiple rows with information and this method of getting the content draws
all and adds it to a single row of output.

I want it to draw the information as multiple rows and echo it out as multiple rows.

I hope I make sense.

Regards,
Thomas

Re: Add a "Linebreak" in a .txt when there is a linebreak in

Posted: Tue Nov 08, 2011 1:07 pm
by Celauran
You mean something like this?

Code: Select all

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

while (!feof($fh))
{
    $line = fgets($fh, 1024);
    echo "$line<br />";
}

Re: Add a "Linebreak" in a .txt when there is a linebreak in

Posted: Tue Nov 08, 2011 1:21 pm
by Goofan
I can't get it to work it makes an infinite loop.

I found an easier way to explain.
I want it to load it line by line (But fast and good)
Then display the lines line by line with a simple <br> in between

Re: Add a "Linebreak" in a .txt when there is a linebreak in

Posted: Tue Nov 08, 2011 1:25 pm
by Celauran
Works just fine for me, no infinite loop.

Same idea:

Code: Select all

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

while (($line = fgets($fh, 1024)) !== FALSE)
{
    echo "$line<br />";
}

Re: Add a "Linebreak" in a .txt when there is a linebreak in

Posted: Tue Nov 08, 2011 1:27 pm
by Goofan
Strange second worked.
first didn't

Thank you for the help. (I tried all these before but always got an infinite loop.)

Regards,
Thomas