| | |
read-delete lines from a text file
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 0
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
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 )
I figured out how to separate the actual file into the component parts with the following script:
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? 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 info, find the appropriate line, delete the entire line and then rewrite the file?? Where do I go from here??? Any help would be GREATLY appreciated. Thank you in advance!
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
PHP Syntax (Toggle Plain Text)
<? // 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 )
I figured out how to separate the actual file into the component parts with the following script:
PHP Syntax (Toggle Plain Text)
<? $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>"; } ?>
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? 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 info, find the appropriate line, delete the entire line and then rewrite the file?? Where do I go from here??? Any help would be GREATLY appreciated. Thank you in advance!
Hi wrstrong,
Looks like someone else is having the same problem you are... This is the thread that was produced for review:
http://www.webmasterworld.com/php/3304270.htm
And here is the final result PHP that was used to manipulate the flat file database:
Nice solution as far as I can tell, hope it helps you out.
Looks like someone else is having the same problem you are... This is the thread that was produced for review:
http://www.webmasterworld.com/php/3304270.htm
And here is the final result PHP that was used to manipulate the flat file database:
PHP Syntax (Toggle Plain Text)
<?php $selLine = $_GET['id']; $delType = $_GET['type']; $file = "store/" . $delType . ".txt"; $news = file($file); $cnt = 0; foreach ($news as $key => $line) { if ($key!= $selLine) { $result[] = $line; } $cnt+1; } if ($key!= 0) { $fh = fopen($file, "w"); foreach ($result as $nyhet) { fwrite($fh, $nyhet); } fclose($fh); } elseif ($key == 0) { $fh = fopen($file, "w"); fwrite($fh, ""); fclose($fh); } ?>
Nice solution as far as I can tell, hope it helps you out.
Someone had asked the same question here.
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 0
Thanks for the quick reply! Although it might seem like it's that simple, I must be really dense tonight (or overly tired). LOL Here's where I am so far...
As I already know how to put the text into the comments.txt, I'm not going to include it here.
comments.txt
readtest.php (simply reads the file, displays it & provides a link for deletion)
delete_entry.php (When used as delete_entry.php?entry=3, for example, it should delete the third entry.)
So far, the best that I can get as far as output is...
{path} is the actual path on the server (not important).
Have I missed something? I need to use a flat file instead of a mysql database or a microsoft office mdb file because it needs to be quickly editable by ANY system at will and the EASIEST solution in that case was to make sure that everyone who needs to edit it will have the tools that they need WITHOUT installing anything or knowing any type of, 'special something' in order to do what they need to do.
I am usually a quick learner; however, I've searched & read everything I can on the subject and I'm stuck at this point.
Thank you for all your help thus far!
As I already know how to put the text into the comments.txt, I'm not going to include it here.
comments.txt
PHP Syntax (Toggle Plain Text)
Here is the first title!~12.31.2007~2:14 pm~Here is the first entry! Here is the second title!~12.31.2007~2:14 pm~Here is the second entry! Here is the third title!~12.31.2007~2:14 pm~Here is the third entry! Here is the fourth title!~12.31.2007~2:14 pm~Here is the fourth entry! Here is the fifth title!~12.31.2007~2:14 pm~Here is the fifth entry! Here is the sixth title!~12.31.2007~2:14 pm~Here is the sixth entry! Here is the seventh title!~12.31.2007~2:14 pm~Here is the seventh entry! Here is the eighth title!~12.31.2007~2:14 pm~Here is the eighth entry! Here is the ninth title!~12.31.2007~2:14 pm~Here is the nineth entry! Here is the tenth title!~12.31.2007~2:14 pm~Here is the tenth entry!
readtest.php (simply reads the file, displays it & provides a link for deletion)
PHP Syntax (Toggle Plain Text)
<? print ("<table border=0>"); $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-1; $i++) { $comment = explode("~", $lines[$i]); $new_entry[$i] = $comment[0] . "<br>" . $comment[1] . " @ " . $comment[2] . "<br>" . $comment[3] . "<br>"; print ("<tr> <td width=60%>$new_entry[$i]</td> <td> <p align=center><a href=delete_entry.php?entry="); print $i+1; print (">delete</a></td> </tr> <tr> <td colspan=2><hr size=1></td> </tr>"); } print ("</table>"); ?>
delete_entry.php (When used as delete_entry.php?entry=3, for example, it should delete the third entry.)
PHP Syntax (Toggle Plain Text)
<? $filename = 'comments.txt'; $fp = fopen( $filename, 'r+' ); $file_contents = @fread( $fp, filesize( $filename ) ); $lines = explode("\n", $file_contents); $numlines = count($lines); for ($i = 0; $i < $numlines-1; $i++) { $comment = explode("~", $lines[$i]); if (!($comment[$i] == $entry)) { $together .= $comment[$i]; if (!($i = $numlines)) { $together .= "~"; } } else { } } fwrite ( $filename , $together ); fclose( $fp ); ?>
So far, the best that I can get as far as output is...
•
•
•
•
Warning: fwrite(): supplied argument is not a valid stream resource in {path}\delete_entry.php on line 23
Have I missed something? I need to use a flat file instead of a mysql database or a microsoft office mdb file because it needs to be quickly editable by ANY system at will and the EASIEST solution in that case was to make sure that everyone who needs to edit it will have the tools that they need WITHOUT installing anything or knowing any type of, 'special something' in order to do what they need to do.
I am usually a quick learner; however, I've searched & read everything I can on the subject and I'm stuck at this point.
Thank you for all your help thus far!
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 0
What's the difference between::
and
Other than the obvious that there is space in the one where it's not in the other and that the r+ says read and write from the beginning of the file & the w+ says read & write from the erasing of the entire file? Why are the differences so critical??
PHP Syntax (Toggle Plain Text)
$filename = 'comments.txt'; $fp = fopen( $filename, 'r+' ); :::: Then stuff in between :::: fwrite ( $filename , $together ); fclose( $fp );
and
PHP Syntax (Toggle Plain Text)
$filename = 'comments.txt'; $fp = fopen( $filename, 'w+' ); fwrite($fp,$together); fclose($fp);
Other than the obvious that there is space in the one where it's not in the other and that the r+ says read and write from the beginning of the file & the w+ says read & write from the erasing of the entire file? Why are the differences so critical??
In your case, you are trying to write the contents of $together directly to a file which wouldn't work. In my case, I am writing it to the file using the file pointer $fp. Using modes r+, w+ depends on how you want to handle the file.
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
![]() |
Similar Threads
- delete first line from a text file (C++)
- write and delete details on csv file in C++ (C++)
- Finally, how to remove the d8t.biz Explorer hijack, FOREVER! (Viruses, Spyware and other Nasties)
- help about programming (C++)
Other Threads in the PHP Forum
- Previous Thread: PHP Doubly Linked List
- Next Thread: Help beta testing new CMS
| Thread Tools | Search this Thread |
5.2.10 action apache api array beginner binary broken cakephp checkbox class classes cms code cron curl database date destroy display dynamic echo echo$_get[x]changingitintovariable... email encode error fcc file files folder form forms function functions google header howtowriteathesis href htaccess html if-else image images include insert ip javascript joomla limit link local login mail memberships menu mlm mod_rewrite multiple multipletables mysql mysqlquery neutrality oop open passwords paypal pdf php provider query radio random record remote rss script search server sessions sockets source space sql strip_tags syntax system table template thesishelp tutorial update upload url validator variable video voteup web window.onbeforeunload=closeme; youtube






