Jump to content

Find and delete row in file


dreamwest

Recommended Posts

Im trying to edit a file where some lines end the same way:

 

.....line1..example/line

.....line2..example/line

 

How can i find and delete these specific lines from a file?

 

All ive got so far is the file open:

 

<?php
$file = fopen("example.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
  {
  echo fgets($file). "<br />";
  }
fclose($file);
?>

Link to comment
https://forums.phpfreaks.com/topic/149501-find-and-delete-row-in-file/
Share on other sites

Dont know if that will work, that will only take off the similar endings not the whole line

 

Each line is different except for the ending :

 

line 1 description| example ending

line 2 has separate description but same ending | example ending

line 3 is different to line 1 &2 also has a different ending | dont delete this line

 

I just want to delete the whole line that contains "example ending"

//Output a line of the file until the end is reached
if( file_exists( "example.txt" ) )
{
$fileContents = file( "example.txt" );
$searchFor = "hello";
for( $i = 0; $i < count( $fileContents ); $i++ )
{
	if( strstr( $fileContents[$i], $searchFor ) )
		unset( $fileContents[$i] );
}

echo implode( "<br />", $fileContents );
}
else
{
echo "File doesn't exist.";	
}

 

You'll obviously have to save the file afterwards, but I thought for testing you're best to leave it out until you're happy. :)

  • 4 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.