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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

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

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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 />";
}
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

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

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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 />";
}
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

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

Post 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
Post Reply