I know I can do this:

//read the entire string
$str=implode("\n",file('somefile.txt'));

$fp=fopen('somefile.txt','w');
//replace something in the file string - this is a VERY simple example
$str=str_replace('Yankees','Cardinals',$str);

//now, TOTALLY rewrite the file
fwrite($fp,$str,strlen($str));

but, is there a more elegant and smarter way to rewrite a file that involves moving the cursor and replacing just a section of text? Seems that rewriting the file to replace one word is not smart

TIA,
samuel

    Well, for one, right now, you're just recreating the entire file. You could use fopen('filename.txt', 'a'); however, you'd still have to search in there and replace it. There is no "easy" way to do it. I think what you have is fine.

      Write a Reply...