I have a text file which is basically the basis for a shopping basket mechanism. I need to delete a user defined line and am stuck on how to do this - I know I need to read the contents of the file and then rewrite it without 'the' line, but how is this done in code? I would like to be able to do this using the line number as there may be lines with duplicate info on.
Any help much appreciated...
Rebajas.
If the file is not too big:
$file = file($filename); $removedLine = array_splice($file, $lineIndex, 1); $f = fopen($filename, "w"); foreach($file as $line) fputs($f, $line); fclose($f);
and if you don't know which line number it is, but you do know wjhat the line should look like:
$aFile = file($sFilename); $f = fopen($filename, "w"); foreach($file as $line) if ($sLine != 'the line you want to get rid of') { fputs($f, $sLine); }; fclose($f);
You are a star...
🙂 Cheers.