title: How do I edit a certain part of a file through php?
body: <p>I want to edit certain parts of a file before it gets downloaded. I want to do it via php, but i can live with javascript or flash. How wold i do it?</p>
  
  <p>this is how i tried it, but it replaces the whole line and also doesnt revert back to the original</p>
  
  <pre><code>&lt;?php
  $file = 'folder/file.ext';
  
  if($_POST['filename'] != null)
  {
  $exclude = "text"; 
   $lines = file($file); 
   $out = $_POST['filename']; 
   foreach ($lines as $line) { 
    if (strstr($line, $exclude) == "") { 
     $out .= $line; 
    } 
   } 
   $f = fopen($file, "w"); 
   fwrite($f, $out); 
   fclose($f); 
  if (file_exists($file)) {
      header('Content-Description: File Transfer');
      header('Content-Type: application/octet-stream');
      header('Content-Disposition: attachment; filename="text.php"');
      header('Expires: 0');
      header('Cache-Control: must-revalidate');
      header('Pragma: public');
      header('Content-Length: ' . filesize($file));
      readfile($file);
      exit;
      $exclude = $_POST['filename']; 
      $lines = file($file); 
      $out = "text"; 
      foreach ($lines as $line) { 
          if (strstr($line, $exclude) == "") { 
          $out .= $line; 
      } 
      } 
   $f = fopen($file, "w"); 
   fwrite($f, $out); 
   fclose($f); 
  }
  
  }
  ?&gt;
  </code></pre>