Jump to content

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...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.