Re: Manioulating a Flat File
| From: | 1LT John W. Holmes | Date: | Thu, 17 Oct 2002 19:38:21 +0000 |
| Subject: | Re: Manioulating a Flat File | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-22484@lists.php.net to get a copy of this message | ||
> Here's what I'd like to know: Is there a PHP function for easily finding
and removing a line >from a flat file of data. And, if not, is there a way
to remove a line (or lines) after finding them >using fget()?
Easy way to do this is with file(). It'll read the entire file into an
array, with each array element being a line of the file. Use array_search()
to find the line you're looking for, then unset() that element.
//line to look for
$search = "...";
$file = file("file.txt");
$key = array_search($search,$file);
if($key === FALSE)
{ echo "line not found"; }
else
{ unset($file[$key]); }
//write corrected file back to disk
$fp = fopen("file.txt");
fwrite($fp,implode("\n",$file);
fclose($fp);
Adapt to your needs...
---John Holmes...