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
phpbeginer
Forum Newbie
Posts: 8 Joined: Sun Feb 15, 2004 1:24 am
Post
by phpbeginer » Sun Feb 15, 2004 1:24 am
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.
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Sun Feb 15, 2004 1:27 am
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 » Sun Feb 15, 2004 1:29 am
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 » Sun Feb 15, 2004 1:30 am
above code is just a test! before I start writing my long DB file ..
Thanks.
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Sun Feb 15, 2004 1:32 am
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 » Sun Feb 15, 2004 1:36 am
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.
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Sun Feb 15, 2004 1:38 am
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 » Sun Feb 15, 2004 1:41 am
Thank you Mark. That was so much helpful.
yes, It works now..
Regards.
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Sun Feb 15, 2004 1:41 am
No problem, good luck with the learning