Re: delete lines from text file
| From: | R'twick Niceorgaw | Date: | Fri, 06 Jun 2003 04:04:23 +0000 |
| Subject: | Re: delete lines from text file | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-150447@lists.php.net to get a copy of this message | ||
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