new line in echo statement

jammin's picture

They have: 222 posts

Joined: Sep 2002

i took some time off c++ to learn a little more php, and i decided to make a simple little thing that stores url's in a .txt file and then outputs them later as links, i did this fine, but i cant get the echo statement to drop down a line after i output the url.

<?php
$filename
= 'test.txt';
$handle = fopen($filename, 'r');
while(!
feof($handle))
{
    
$text= fgets($handle);
    echo \
"<a href=\\"$text\\">\";
    echo
$text;
}

fclose(
$handle);
?>

i tried adding \n to the echo but i cant figure out how to do it right, i appriciate the help.

anyone can do any amount of work provided it isnt the work they are supposed to be doing.

Busy's picture

He has: 6,151 posts

Joined: May 2001

just put the \n where you want the newline to go, is better in a quoted or bracketed section
echo "\n"; or you can add it before the $text (second line), can even make it one whole line
echo "
\n$text";

don't forget to close the link ()

jammin's picture

They have: 222 posts

Joined: Sep 2002

thanks, but it still doesnt work. it wont put out a new line.

dk01's picture

He has: 516 posts

Joined: Mar 2002

Are you on a mac or unix machine?
It matters and actually windows newlines are \r\n.
Anyways for a solution go to this page:
http://www.php.net/manual/en/function.nl2br.php
and look at the comment by eightraks at linux-foo dot com
-dk

He has: 1,758 posts

Joined: Jul 2002

do you mean it wont do a newline when you display it in a browser?

Thats cos its html! you need a

<?php
$filename
= 'test.txt';
$handle = fopen($filename, 'r');
while(!
feof($handle))
{
    
$text= fgets($handle);
    echo \
"<a href=\\"$text\\">\";
    echo
$text;
    echo \"<br>\";
}

fclose(
$handle);
?>

Andy

dk01's picture

He has: 516 posts

Joined: Mar 2002

Yeah if you meant in the browser then look at andy's example and forget mine. I thought you meant when it actually prints to the html source.
-dk

jammin's picture

They have: 222 posts

Joined: Sep 2002

thanks, but i was wondering about the \n thingy, so it just doesnt work when outputting to html?

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

It will work in the HTML source, but not in the displayed HTML. Of course Andy's example is missing a closing tag, lol.

\r\n works for text areas, et cetera.

You want the HTML to look like this:

text
text
text

Or

text
text
text

???

He has: 1,758 posts

Joined: Jul 2002

Quote: It will work in the HTML source, but not in the displayed HTML. Of course Andy's example is missing a closing tag, lol.

True... but then, so was the original example! Wink

Basically the puts the line break in the html, so when the page is viewed in a web browser your content will go to the next line. the /n will put a linebreak in the code so it will split the source code (ie what you see when you go to "view source") into multiple lines

if you don't put a /n after the the source will look like

blah blah blah<br>blah blah blah<br>blah blah blah<br>'

with the /n it will look like:

blah blah blah<br>
blah blah blah<br>
blah blah blah<br>
'

Andy

jammin's picture

They have: 222 posts

Joined: Sep 2002

ahhh... ok i thought maybe it would output to html when it was parsed. thanks.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.