Page 1 of 1

editing files

Posted: Thu Aug 19, 2004 2:48 pm
by Fredix
hey guys, I'm a little messed up with reading/writing files. How would you acomplish the task the replace a special line in a given file where the line is something like $name = "bla"; and has to be replaced with another name?
Surely I could read the entire file into a variable use str_replace and then write the whole file back but isn't there a more elegant way??? (i.e. reading line by line and editing just the current line?)

Posted: Thu Aug 19, 2004 2:59 pm
by feyd
you can either do line-by-line, or the entire file in one hit, however, it may change things unexpectedly..

I've used [php_man]file[/php_man]() and [php_man]file_get_contents[/php_man]() to read the files in, and [php_man]fopen[/php_man]/[php_man]fwrite[/php_man]/[php_man]fclose[/php_man] (since I'm not using php5 yet) to drop them out..

Posted: Thu Aug 19, 2004 3:01 pm
by Fredix
you can either do line-by-line
how??
however, it may change things unexpectedly..
like what?

Posted: Thu Aug 19, 2004 3:05 pm
by feyd
my second line lists the function(s) needed for line-by-line.

The unexpected changes can be from a search and replace that's very loose, or from several occurances of the search, but you only want to change 1 particular one..