Re: Editing files by line

From: Date: Thu, 11 Jul 2002 05:32:47 +0000
Subject: Re: Editing files by line
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-107026@lists.php.net to get a copy of this message
On Thu, Jul 11, 2002 at 02:11:30PM +1000, Martin Towell wrote: >> From: Chris Earle [mailto:chris@earleweb.com] >> >> I was wondering if there was a function that would allow me to "jump" >> to a certain line if a file and then write from there? I'm searching >> php.net without any luck :(. > > I dodn't know of any single function that does what you want, but you can do > this: > > $contents = file($filename); // each line has it's own array position > $contents[$line] = $newline; That's nice, except you could start overwriting existing data. If you're only interested in appending to the end of a file, then use $fp = fopen('filename', 'a+'); and fputs() all you want. But, if you want to stick stuff into the middle... # Real process uses file(), # but for test, set Content manually. $Content[] = 'The start of the content array.'; $Content[] = 'Some more stuff.'; $Content[] = 'And yet more stuff.'; $Location = array_search('Some more stuff.', $Content); $New[] = 'Why, here is some new stuff'; $Final = array_merge( array_slice($Content, 0, $Location + 1), $New, array_slice($Content, 0 - (count($Content) - $Location - 1)) ); # In your process, you'll really want to insert # implode('', $Final) into the file with fputs(), # but, hey, this is a demonstration. print_r($Final); --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409

« previous php.general (#107026) next »