Re: Saving an array as a file
| From: | David Robley | Date: | Thu, 02 Nov 2000 01:19:03 +0000 |
| Subject: | Re: Saving an array as a file | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-23287@lists.php.net to get a copy of this message | ||
On Thu, 02 Nov 2000, Jeff Demel wrote:
> Okay, here's my last dumb newbie question for today - maybe.
>
> I see how to use file() to open a text file as an array of strings with each
> line an array element. That's great. So I make my changes with whatever
> code to one or more array elements. Now, um, what do I do to save the
> changes back to the file?
>
> Example (not exactly what I'm really doing, but way simplified for clarity):
>
> //open file as an array
> $arrTextFile = file("test.txt");
> //modify array
> $arrText[0] = ereg_replace("this","that",$arrText[0]);
> $arrText[1] = ereg_replace("this other","that other",$arrText[1]);
> //save modified array back to test.txt file
> ??so what goes here??
>
> Maybe there's a better way to do this, I don't know. But I need to find a
> way to 1)find a specific line in a file (by matching a string value), modify
> a different portion of that line only - replacing part of the string with
> something else, and then saving the file.
>
> TIA
>
> -Jeff
Open your target file with fopen
For each element of the array
write the element ot the file with fputs or fwrite
Close file with fclose
You nned to watch the mode when opening the file. And pretty much this is
the way to do it; load your file into an array, search the array until you
find the things you want, make the change and save the file.
--
David Robley | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/
AusEinet | http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA