Jump to content

read/delete entire line in text file


wrstrong

Recommended Posts

Hello everyone!

 

I'm have a text file's contents, its output in the browser and a php file (which I'm including below.)  What I need to do is search for a specific line in the text file and delete it.  The problem is that the end result will have a text file with user's comments and a page will be generated that will show all comments, a check box for marking any particular comment for deletion and when the submit box will be pressed, the entry will be deleted (the entire line of the file.) 

 

Here's what I have so far...

 

php file to read the comments.txt

<?
// get comments file
$filename = 'comments.txt';
$fp = fopen( $filename, 'r' );
$file_contents = @fread( $fp, filesize( $filename ) );
fclose( $fp );

// print comments file's file name (not going to be used in the end product)
print ("filename<br>");
print $filename;
print ("<br><hr><br>");
// demo to show how the file is visible
print ("show file<br>");
include 'comments.txt';

// the problem comes when you look at the actual file and its formatting compared to the output
?>

 

The comments.txt file itself (it's ONLY for testing, not the actual end result file)

 

1|<b>this is a test</b>|<br><i>12.31.07 1:45 pm</i>|<br>comment #1<br><br>

1|<b>this is another test</b>|<br><i>12.31.07 2:14 pm</i>|<br>comment #2<br><br>

1|<b>this is yet another test</b>|<br><i>12.31.07 2:14 pm</i>|<br>comment #3<br><br>

1|<b>this is and yet another test</b>|<br><i>12.31.07 1:45 pm</i>|<br>comment #4<br><br>

1|<b>this is still another test</b>|<br><i>12.31.07 2:14 pm</i>|<br>comment #5<br><br>

1|<b>this is the last test</b>|<br><i>12.31.07 2:14 pm</i>|<br>comment #6<br><br>

 

The output on the browser

filename

comments.txt

 

--------------------------------------------------------------------------------

 

show file

1|this is a test|

12.31.07 1:45 pm|

comment #1

 

1|this is another test|

12.31.07 2:14 pm|

comment #2

 

1|this is yet another test|

12.31.07 2:14 pm|

comment #3

 

1|this is and yet another test|

12.31.07 1:45 pm|

comment #4

 

1|this is still another test|

12.31.07 2:14 pm|

comment #5

 

1|this is the last test|

12.31.07 2:14 pm|

comment #6

 

The output is in the following format:

 

1|this is a test| ( and ones following )

Date time|

comment #x ( actual comments )

 

As it is unknown what the actual comments (date/time/actual comment) will be, how do I search & delete something that I do not know what the contents are?  Where do I go from here???  Any help would be GREATLY appreciated.  Thank you in advance!

 

ps-  I just figured out how to separate the actual file into the component parts with the following script:

<?
$filename = 'comments.txt';
$fp = fopen( $filename, 'r' );
$file_contents = @fread( $fp, filesize( $filename ) );
fclose( $fp );
$lines = explode("\n", $file_contents);
$numlines = count($lines);
for ($i = 0; $i < $numlines; $i++) { 
$comment = explode("|", $lines[$i]);
print "This is the comment title:";
print $comment[1];
print "<br>";
print "This is the comment date - time:";
print $comment[2];
print "<br>";
print "This is the comment text:";
print $comment[3];
print "<br>";
}
?>

 

That's part of the way.  I guess what I need to do now is to search for the matching article, delete the entire line & then rewrite the file.  I can search (as noted in above code snippets); however, how do I take that, delete the entire line and then rewrite the file??

Link to comment
https://forums.phpfreaks.com/topic/83976-readdelete-entire-line-in-text-file/
Share on other sites

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.