RE: [PHP] Saving an array as a file

From: Date: Wed, 01 Nov 2000 22:29:43 +0000
Subject: RE: [PHP] Saving an array as a file
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-23259@lists.php.net to get a copy of this message
I've never used serialize/unserialize before, but from what I understand it converts arrays to strings and strings to arrays, which may help me, although I don't know how. Basically, I need to perform a function that's exactly the opposite of file(). I have an existing file that I'm reading into an array using file(), which puts each line of the file into its own array element as a string. Then I'm looking (searching with ereg()) through each line (array element) for a specific unique keyword. When I hit on the correct element, I modify a different part of that line only (using ereg_replace() to replace a part of the string). Once I modify the array element, I need to save the modified array back to the file from which it came. -Jeff -----Original Message----- From: Michael Kimsal [mailto:michael@tapinternet.com] Sent: Wednesday, November 01, 2000 4:04 PM To: Jeff Demel Cc: php-general@lists.php.net Subject: Re: [PHP] Saving an array as a file I may not be understanding exactly what you're looking to do. You have an array that you want to save to disk, right? Is it for storage only, or does it need to be readable/parseable by a person or another process? If it's for storage only, use serialize/unserialize $x = serialize($arrayname); echo $x; // neat bunch of stuff here $arrayname = unserialize($x); Does this help at all? 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 > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net -- ========================== Michael Kimsal http://www.tapinternet.com 734-480-9961

« previous php.general (#23259) next »