Re: Text file

From: Date: Mon, 31 May 2004 18:43:26 +0000
Subject: Re: Text file
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-187304@lists.php.net to get a copy of this message
alantodd wrote:
What would be the easiest way to open a text file, remove a line if the line begins with a word from an array of words (there are 5 words I will be looking for) then write that back to the text file with everything removed
<?php $lookfor = array('one','two','three','four','five'); $pattern = implode('|',$lookfor); $filedata = file('yourfile.txt'); foreach($filedata as $key => $line) { if(preg_match("/^($pattern)/i",$line)) { unset($filedata[$key]); } } $newfiledata = implode("\n",$filedata); $fp = fopen('yourfile.txt','w'); fwrite($fp,$newfiledata); fclose($fp); ?> Untested, but something like that. Read file into an array, loop through each line trying to match word at beginning. If there's a match, remove line, then write new data at the end. -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals – www.phparch.com

« previous php.general (#187304) next »