Page 1 of 1

new line character

Posted: Sun Feb 15, 2004 1:24 am
by phpbeginer
Hi Everybody,

I am using PHP version 4.3.3. I'm trying to write a new line into a file using "\n" but it does not work with me?
I used "\t" and it wrote the tab space into the file. Please advise!

Thanks in a dvance.

Posted: Sun Feb 15, 2004 1:27 am
by markl999
Got a code snippet to show? The bit where you try to write the newline.

Posted: Sun Feb 15, 2004 1:29 am
by phpbeginer
Wow.. Thanks for the instance reply.. anyway, here is the code:

<?php
$fp = fopen( "C:\\dbfile.txt" , "w" );
fwrite($fp,"\n");
fwrite($fp,"<head>\n");
fwrite($fp,"</head>\n");
fwrite($fp,"<body>\n");
fwrite($fp,"This is my PDF");
fwrite($fp,"</body>\n");
fwrite($fp,"\n");
fclose($fp);
?>

Regards.

Posted: Sun Feb 15, 2004 1:30 am
by phpbeginer
above code is just a test! before I start writing my long DB file ..

Thanks.

Posted: Sun Feb 15, 2004 1:32 am
by markl999
That works fine, as expected. If you're viewing dbfile.txt in a browser then you won't see the newlines. You need to convert them to <br />'s first.

So if you do want to view the fiel in a browser then you could do..
<?php
echo nl2br(file_get_contents('dbfile.txt'));
?>

Posted: Sun Feb 15, 2004 1:36 am
by phpbeginer
Actually, I am opening the file from C drive "Double Clicks" and I don't see the newlines there? but I can see tabs!! any explanation ?

Thanks.

Posted: Sun Feb 15, 2004 1:38 am
by markl999
Yup, windows uses carriage return line feed for a newline, so instead of using \n use \r\n

Posted: Sun Feb 15, 2004 1:41 am
by phpbeginer
Thank you Mark. That was so much helpful.

yes, It works now..

Regards.

Posted: Sun Feb 15, 2004 1:41 am
by markl999
No problem, good luck with the learning ;)