Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Echoing line breaks with php fgets()

Hello..

What if I have a text file with strings of varying lengths like this...?

lorem
lorem ipsum
l 
lorem of ipsum
lorem
lore 
lorem ipsum

I'm trying to use fgets() to echo the contents of the file in exactly that way but it only outputs in 1 one line.

lorem lorem ipsum l lorem of ipsum lorem lore lorem ipsum

Is there a way I can do this with fgets? I've even tried modifying HTTP to automatically detect line endings I've scoured the net with no success. :)

<?php
    $file = "file.txt";

    //open connection to file

        if($fh = fopen($file, 'r')) {
            while (!feof($fh)) {
                echo $line = fgets($fh) . "/n";
            }
        } else {
               echo "Unable to open file: " . $file;
        }  
?>

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Your code works for me with a few minor adjustments. First, you start the file with <? but that should be <?php. Your code does not run for me until I change that part. Also, you're confusing "/n" with "\n". The first will literally print out a forward slash and an "n" while the second prints out a newline character. But that isn't even needed. the echo $line writes everything to a new line with each iteration.

Hope this helps! :sparkles:

edited for update: After conversing a bit, we came to the conclusion that while it was echoing out on separate lines to the console, it wasn't echoing out separate lines to the browser. The solution was to concatenate a <br />.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Yea I hastily added the <? to my code forgetting the PHP when I realised the PHP formatting wasn't working ;)

I've fixed that it ;)

Similarly with the escape character... I must have just mixed that up when I put that in... but surely the new line character should come into effect at the end of each line but it's not doing so. Hmmm.

Yes, fgets should write a new line automatically but I don't understand why it's not doing that... so trying to add the new line character is trying to force that. Odd,

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Jonathan Grieve Well that's the weird part. It does write it to a new line when I try it in workspaces. Seriously, try it here on the workspaces and then figure out what is different between here and where you're trying it :smiley:

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Nope. All echoed on the one line. Which leads me to thing it's a PHP configuration issue.. but then I've already tried auto new line detect.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Sorted in the end. I just didn't think of concatenating <br /> instead of the new line character sequence since I'm attempting to output to the browser.

Joel Bardsley
Joel Bardsley
31,249 Points

If you're on a Windows machine, have you tried echoing "\r\n" instead of "\n" to see if it makes a difference? Doesn't seem right having to rely on using <br /> tags for something like this!