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
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php