[thelist] preserving line breaks in a text file

Mark Mckee lists at soddengecko.com
Mon Jan 15 14:58:28 CST 2007


hi all

this was previously known as "text file creation from user form" but i 
have since surpassed that and have what i need,

i am however having some trouble preserving the line breaks. as the 
output of the file is just a plain text file, i do not need the nl2br 
function adding in some <br /> wherever a line break occurs. i have not 
been able to find anything on this that has helped

anyone have any suggestions?

here is the code i am using

<?php
   $guestbook = "files/$title.txt";
   error_reporting (E_ALL ^ (E_NOTICE | E_WARNING));
   if ($_POST['submit']) {
     # "a" for append
     $file = fopen($guestbook, "a");
     if (!$file) {
       echo "Can't write to guestbook file";
       die;
     }
     # Grab the form fields
     $title = $_POST['title'];
     $data = $_POST['data'];
     # Remove dangerous characters, and limit the length
     $title = clean($title, 40);
$data=nl2br($data);

     fwrite($file,
       "$data\n");
     fclose($file);
   }

   function clean($title, $max) {
     # Turn tabs and CRs into spaces so they can't
     # fake other fields or extra entries
     $title = ereg_replace("[[:space:]]", ' ', $title);
     # Escape < > and and & so they
     # can't mess withour HTML markup
     $title = ereg_replace('&', '&amp;', $title);
     $title = ereg_replace('<', '&lt;', $title);
     $title = ereg_replace('>', '&gt;', $title);
     # Don't allow excessively long entries
     $title = substr($title, 0, $max);
     return $title;
   }
?>
<form action="<?php $_SERVER['php self']  ?>" method="POST">
<input name="title" maxlength="40">
<br /><br />
<textarea name="data" cols="30" rows="30" maxlength="6000"></textarea>
<br /><br />
<input type="submit" name="submit" value="Submit">



More information about the thelist mailing list