new line character

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
phpbeginer
Forum Newbie
Posts: 8
Joined: Sun Feb 15, 2004 1:24 am

new line character

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Got a code snippet to show? The bit where you try to write the newline.
phpbeginer
Forum Newbie
Posts: 8
Joined: Sun Feb 15, 2004 1:24 am

Post 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.
phpbeginer
Forum Newbie
Posts: 8
Joined: Sun Feb 15, 2004 1:24 am

Post by phpbeginer »

above code is just a test! before I start writing my long DB file ..

Thanks.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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'));
?>
phpbeginer
Forum Newbie
Posts: 8
Joined: Sun Feb 15, 2004 1:24 am

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Yup, windows uses carriage return line feed for a newline, so instead of using \n use \r\n
phpbeginer
Forum Newbie
Posts: 8
Joined: Sun Feb 15, 2004 1:24 am

Post by phpbeginer »

Thank you Mark. That was so much helpful.

yes, It works now..

Regards.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

No problem, good luck with the learning ;)
Post Reply