Re: [PHP] Editing files by line

2002-07-11 Thread Chris Earle

Cool thanks, that probably will make my code execute a decent bit faster (as
the file gets larger).

But the array_search function never works for me (don't know why, I know
exactly what I'm searching for and that it is there), so I made my own
function..

//Find and return first occurance of a string ("$Find") in any line of
$LineArray
function FindKey($LineArray, $Find)
{
 foreach($LineArray as $key => $value)
 {
  if (ereg($Find, $value))
   return $key;
 }

 return FALSE;
} // END OF FindKey(...);

I think this combined with your code should help me a lot, thanks a ton.

"Analysis & Solutions" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> On Thu, Jul 11, 2002 at 02:11:30PM +1000, Martin Towell wrote:
> >> From: Chris Earle [mailto:[EMAIL PROTECTED]]
> >>
> >> 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



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Editing files by line

2002-07-10 Thread Analysis & Solutions

On Thu, Jul 11, 2002 at 02:11:30PM +1000, Martin Towell wrote:
>> From: Chris Earle [mailto:[EMAIL PROTECTED]]
>> 
>> 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Editing files by line

2002-07-10 Thread Chris Earle

That's what I was talking about :).  I wish there was something built in
that would just let me append a file where I want, that would make things a
little easier.  Oh well, thanks a lot for the help that makes sense (and I
think it works).

"Martin Towell" <[EMAIL PROTECTED]> wrote in message
6416776FCC55D511BC4E0090274EFEF508A5FD@EXCHANGE">news:6416776FCC55D511BC4E0090274EFEF508A5FD@EXCHANGE...
> 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;
> $content = implode("\n", $contents);
> $f = fopen($filename, "w");
> fputs($f, $content);
> fclose($f);
>
>
> (not tested, but should work as is) some error checking would also be
> good :)
>
> -Original Message-
> From: Chris Earle [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, July 11, 2002 2:10 PM
> To: [EMAIL PROTECTED]
> Subject: [PHP] Editing files by line
>
>
> 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 :(.
>
> Also, if there is not specific function, is there a trick that gives the
> same effect (without reading the entire file line by line, then writing to
> it where I need to, then replacing the file)?
>
> Thanks in advance (hopefully :)).
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Editing files by line

2002-07-10 Thread Martin Towell

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;
$content = implode("\n", $contents);
$f = fopen($filename, "w");
fputs($f, $content);
fclose($f);


(not tested, but should work as is) some error checking would also be
good :)

-Original Message-
From: Chris Earle [mailto:[EMAIL PROTECTED]]
Sent: Thursday, July 11, 2002 2:10 PM
To: [EMAIL PROTECTED]
Subject: [PHP] Editing files by line


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 :(.

Also, if there is not specific function, is there a trick that gives the
same effect (without reading the entire file line by line, then writing to
it where I need to, then replacing the file)?

Thanks in advance (hopefully :)).



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php