Re: Saving an array as a file

From: Date: Thu, 02 Nov 2000 16:43:29 +0000
Subject: Re: Saving an array as a file
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-23422@lists.php.net to get a copy of this message
> Sorry to be such a pain, but this is all new stuff to me. No worries - I'm here to help! <? // this will OVERWRITE the file each time // to APPEND to the file use a+ $fp = fopen('/path/to/write/file','w+'); while(list($key,$val) = each($array_of_lines)){ $line_to_write = $val."\n"; fwrite($fp,$line_to_write); } fclose($fp); ?> --Joe > > -Jeff > > -----Original Message----- > From: Joe Stump [mailto:joe@earth.care2.com] > Sent: Thursday, November 02, 2000 10:07 AM > To: Jeff Demel > Cc: php-general@lists.php.net > Subject: Re: [PHP] Saving an array as a file > > > Here is are two ways ... > > > <? > > // way 1 > $file = '/path/to/file' > $fp = fopen($file,'r'); > $contents = fread($fp,filesize($file)); > $array_of_lines = explode("\n",$contents); > fclose($fp); > > // way 2 > $fp = fopen($file,'r'); > while(!feof($fp)){ > $line = trim(fgets($fp,4096)); > $array_of_lines[] = $line; > } > fclose($fp); > > ?> > > --Joe > > > On Wed, Nov 01, 2000 at 03:04:33PM -0600, 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 > ============================================================================ > ==== > = Joe Stump joestump98@yahoo.com > http://www.miester.org = > ============================================================================ > ==== > "Real programmers don't comment their code. > It was hard to write, it should be hard to understand." > > -- > 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 > ================================================================================ = Joe Stump joestump98@yahoo.com http://www.miester.org = ================================================================================ "Real programmers don't comment their code. It was hard to write, it should be hard to understand."

« previous php.general (#23422) next »