Link to home
Start Free TrialLog in
Avatar of brad_tho
brad_tho

asked on

Str_replace and include() together

Hi, I have a document data.txt with csv data and each entry is on a new line.

When I include("data.txt"); all of the data is on one line...
and the following doesnt work either:
str_replace("\n","<br>",include("data.txt"));

Any ideas?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Robin Hickmott
Robin Hickmott

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brad_tho
brad_tho

ASKER

thanks that worked a treat, dont worry i am using fopen() etc for error handling, i just didnt include them in the question because it wasnt really relevant. but thanks though!

<?php
$filename = 'data.txt';
$content = $_POST['procedure'] . "," . $_POST['date'] . "," . $_POST['start'] . "," . $_POST['finish'] . "," . $_POST['surgeon'] . "\r\n";

if (is_writable($filename)) {
   if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
   }
   if (fwrite($handle, $content) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
   echo "Success, wrote ($content) to file ($filename)!<br><br>
     <html>
     <head>
     <title>
     New Entry Submitted!
     </title>
     </head>
     <body>
   ";

  $file = file("data.txt");
  foreach ($file as $value) {
    echo("$value" . "<br>");
  }

  echo "
    </body>
    </html>  
   ";
  fclose($handle);
} else {
   echo "The file $filename is not writable";
}
?>
PHP also features a function called nl2br which replaces the newline character with the <br> element.

But rhickmott is right about include not being ideal for file access.