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?
Open a text file, and remove line?
Moderator: General Moderators
-
slipstream
- Forum Commoner
- Posts: 86
- Joined: Fri Apr 19, 2002 8:53 am
- Location: Canada
-
slipstream
- Forum Commoner
- Posts: 86
- Joined: Fri Apr 19, 2002 8:53 am
- Location: Canada
How about something like
edit: You'll probably have to add a \n to the fwrite. I'm not in a place I can test code right now.
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);-
slipstream
- Forum Commoner
- Posts: 86
- Joined: Fri Apr 19, 2002 8:53 am
- Location: Canada
Simply replace
with
Code: Select all
fwrite($fp,$file[$i]);Code: Select all
fwrite($fp,$file[$i]."\n");