Inserting Line Break

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
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Inserting Line Break

Post by tail »

Here's the code I'm using:

Code: Select all

<?php
$filename = 'visitors.txt';
$screenname = $_GET ['screenname'];
if (is_writable($filename)) {
   if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
   }
   if (fwrite($handle, $screenname) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
   echo "Success, wrote ($screenname) to file ($filename)";
   fclose($handle);
} else {
   echo "The file $filename is not writable";
}
?>
But the problem is I need to have a line break after every value that get's put in the file. Any help?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

"\n"
// or
PHP_EOL // a constant in "later" versions of php
Note, \n must be in double quotes to be processed.
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

Where would I put that?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

they would need to be added while calling fwrite()
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

This isn't working. Can you place it where it needs to be? That would be great.

Code: Select all

<?php
$filename = 'visitors.txt';
$screenname = $_GET ['screenname'];
if (is_writable($filename)) {
   if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
   }
   if (fwrite($handle, $screenname, "\n") === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
   echo "Success, wrote ($screenname) to file ($filename)";
   fclose($handle);
} else {
   echo "The file $filename is not writable";
}
?>
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

this...

Code: Select all

if (fwrite($handle, $screenname, "\n") === FALSE) {

should be....

Code: Select all

if (fwrite($handle, $screenname . "\n") === FALSE) {

you had a comma after the $screenname, it need to be a dot/period, if on Windows you also need to use "\r\n", instead on just "\n" for linux/unix and just "\r" for OSMac!


pif!
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

Thank you :)
Post Reply