editing files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

editing files

Post 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?)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Post by Fredix »

you can either do line-by-line
how??
however, it may change things unexpectedly..
like what?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
Post Reply