RE: [PHP] delete lines from text file
I think you'll have to read the file manually (file() would prob be the best bet) and manually find/delete the line and then rewrite the file. Martin -Original Message- From: Matt Palermo [mailto:[EMAIL PROTECTED] Sent: Friday, June 06, 2003 1:54 PM To: [EMAIL PROTECTED] Subject: [PHP] delete lines from text file Can anyone help me figure out how to search for a string in a text file, then delete the whole line of text that it occurs in? For example: I have a text file which contains the following. //** text.txt ** Keep this line. Also keep this line. Delete this line. Keep this one too. Now I want to do a search on this text file for the word delete and if it finds that word, it will delete the whole line it occurs on. So after this, the text file will look like this one: //** text.txt ** Keep this line. Also keep this line. Keep this one too. Can anyone help me with this? Thanks. Matt -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete lines from text file
On Thursday 05 June 2003 11:53 pm, Matt Palermo wrote: Can anyone help me figure out how to search for a string in a text file, then delete the whole line of text that it occurs in? For example: I have a text file which contains the following. //** text.txt ** Keep this line. Also keep this line. Delete this line. Keep this one too. Now I want to do a search on this text file for the word delete and if it finds that word, it will delete the whole line it occurs on. So after this, the text file will look like this one: //** text.txt ** Keep this line. Also keep this line. Keep this one too. Can anyone help me with this? Thanks. Matt I think there's a better way to do it but here's whaqt I think $f=file($file_name); $new_file_data=Array(); $match_string=delete; while (list($k,$v)=each($f)) { if ( !strstr($match_string,$v)) $new_file_data[count($new_file_data)] = $v; } $fd=fopen($file_name,w); while (list($k,$v)=each($new_file_data)) { fwrite($fd,$v); } HTH -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete lines from text file
Matt, Try something like: ? $fp=fopen(file_name.txt,r); while (feof($fp)) { $line=fget($fp,100); // grabs text to end of line or 100 characters whichever comes first if (!preg_match(/delete/,$line)) { $new_line.=$line; } } fclose($fp); $fp=fopen(file_name.txt,w); fwrite($fp,$new_line) or die (something died in here); fclose($fp); ? I didn't check this before posting so there could be some syntax issues, but the flow is what you need. Open a file for reading, read it line by line to find a text phrase, accumulate the lines (less the one with the phrase) in a new variable, close the file, open it again for writing, write the text to the file, and then close it. Hope this helps, Hugh - Original Message - From: Matt Palermo [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, June 05, 2003 8:53 PM Subject: [PHP] delete lines from text file Can anyone help me figure out how to search for a string in a text file, then delete the whole line of text that it occurs in? For example: I have a text file which contains the following. //** text.txt ** Keep this line. Also keep this line. Delete this line. Keep this one too. Now I want to do a search on this text file for the word delete and if it finds that word, it will delete the whole line it occurs on. So after this, the text file will look like this one: //** text.txt ** Keep this line. Also keep this line. Keep this one too. Can anyone help me with this? Thanks. Matt -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete lines from text file
My bad: while (!feof($fp)) instead of what I wrote earlier. - Original Message - From: Hugh Danaher [EMAIL PROTECTED] To: Matt Palermo [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Thursday, June 05, 2003 10:59 PM Subject: Re: [PHP] delete lines from text file Matt, Try something like: ? $fp=fopen(file_name.txt,r); while (!feof($fp)) { $line=fget($fp,100); // grabs text to end of line or 100 characters whichever comes first if (!preg_match(/delete/,$line)) { $new_line.=$line; } } fclose($fp); $fp=fopen(file_name.txt,w); fwrite($fp,$new_line) or die (something died in here); fclose($fp); ? I didn't check this before posting so there could be some syntax issues, but the flow is what you need. Open a file for reading, read it line by line to find a text phrase, accumulate the lines (less the one with the phrase) in a new variable, close the file, open it again for writing, write the text to the file, and then close it. Hope this helps, Hugh - Original Message - From: Matt Palermo [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, June 05, 2003 8:53 PM Subject: [PHP] delete lines from text file Can anyone help me figure out how to search for a string in a text file, then delete the whole line of text that it occurs in? For example: I have a text file which contains the following. //** text.txt ** Keep this line. Also keep this line. Delete this line. Keep this one too. Now I want to do a search on this text file for the word delete and if it finds that word, it will delete the whole line it occurs on. So after this, the text file will look like this one: //** text.txt ** Keep this line. Also keep this line. Keep this one too. Can anyone help me with this? Thanks. Matt -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] # of lines in a file
You could try... $file = ( /path/file.ext ); $lines = sizeof ( $file ); [EMAIL PROTECTED] wrote: Hey guys, I've been searching the manual for a way to grab the number of lines in a file and read a particular line from it, however I found no solution to my problem so I'm wondering if any of you out there could help me :-) - CS -- By-Tor.com It's all about the Rush http://www.by-tor.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] # of lines in a file
$f = fopen(filename,r); $data = "" fclose($f); $data = "" $lines = count($data); $certainline = $data[linenumberyouwant]; Replace filename in both places, and linenumberyouwant. On Mon, 2002-12-09 at 11:58, [EMAIL PROTECTED] wrote: Hey guys, I've been searching the manual for a way to grab the number of lines in a file and read a particular line from it, however I found no solution to my problem so I'm wondering if any of you out there could help me :-) - CS -- Adam Voigt ([EMAIL PROTECTED]) The Cryptocomm Group My GPG Key: http://64.238.252.49:8080/adam_at_cryptocomm.asc signature.asc Description: This is a digitally signed message part
RE: [PHP] # of lines in a file
Hey guys, I've been searching the manual for a way to grab the number of lines in a file and read a particular line from it, however I found no solution to my problem so I'm wondering if any of you out there could help me file() will read the file into an array, one line per element, so... $line_to_read = 55; $file_array = file('file.txt'); echo $file_array[$line_to_read]; Remember that arrays are zero based. ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] # of lines in a file
Might not be the most secure way, but very fast... $lines = `cat file.txt | wc -l` That will give you the number of lines...but again not the best method... On Mon, 2002-12-09 at 09:58, [EMAIL PROTECTED] wrote: Hey guys, I've been searching the manual for a way to grab the number of lines in a file and read a particular line from it, however I found no solution to my problem so I'm wondering if any of you out there could help me :-) - CS -- Ray Hunter email: [EMAIL PROTECTED] www:http://venticon.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php