dreamwest Posted March 15, 2009 Share Posted March 15, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149501-find-and-delete-row-in-file/ Share on other sites More sharing options...
ram4nd Posted March 15, 2009 Share Posted March 15, 2009 Load the file in string, use str_replace, write string to the file. Quote Link to comment https://forums.phpfreaks.com/topic/149501-find-and-delete-row-in-file/#findComment-785103 Share on other sites More sharing options...
dreamwest Posted March 15, 2009 Author Share Posted March 15, 2009 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" Quote Link to comment https://forums.phpfreaks.com/topic/149501-find-and-delete-row-in-file/#findComment-785113 Share on other sites More sharing options...
Mikedean Posted March 15, 2009 Share Posted March 15, 2009 //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. Quote Link to comment https://forums.phpfreaks.com/topic/149501-find-and-delete-row-in-file/#findComment-785169 Share on other sites More sharing options...
ram4nd Posted April 9, 2009 Share Posted April 9, 2009 Then load it to array as 1 strin is 1 line, use str_replace on every variable, write array to the file. Quote Link to comment https://forums.phpfreaks.com/topic/149501-find-and-delete-row-in-file/#findComment-805677 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.