Page 1 of 1
Open a text file, and remove line?
Posted: Mon Jun 23, 2003 10:28 am
by slipstream
Hi there I need some help on how to delete a line from a text file after I open it. The user will select a string to remove, then I have to open the file and find it and delete the line.
Can someone start me out here?
Posted: Mon Jun 23, 2003 1:34 pm
by slipstream
no one can get me started?
I know I have to do a string compare as I read through each line of the file. Once it is found to be equal, how do I remove it is the Q?
Posted: Mon Jun 23, 2003 1:39 pm
by nielsene
How about something like
Code: Select all
$testLine = $_POST["deleteMe"]; // or whereever it comes from
$file =file($filename); // stick the file into an array of lines
$fp = fopen($filename,"w"); // wipe the old file
$numlines = count($file);
for ($i=0;$i<$numlines;$i++)
{
if ($file[$i]!=$testLine)
{
fwrite($fp,$file[$i]);
}
}
fclose($fp);
edit: You'll probably have to add a \n to the fwrite. I'm not in a place I can test code right now.
Posted: Mon Jun 23, 2003 2:01 pm
by slipstream
worked great, thanks.
Posted: Mon Jun 23, 2003 2:07 pm
by phice