Page 1 of 1

Replace Line in a file?

Posted: Mon Jun 20, 2005 6:11 pm
by mudvein
I have a file I'm trying to do some manipulation to.

Basically, I am trying to check for a variable name in a script, and then I find that variable name, replace the entire line it resides on.

Example.php

Code: Select all

<?php
$my_search_file = 'some dynamic value';
$my_second_search_file = 'another dynamic value';
?>
So, i want to search for $my_search_file, and when I find that file, replace it with a new variable.. Can anyone help me? only thing i got so far is

Code: Select all

<?php
$open_me = file_get_contents($Example.php);
$found_one = substr_count($open_me,'\$my_search_file');
//need code to replace the line here
?>
can anyone help?? thanks

Re: Replace Line in a file?

Posted: Mon Jun 20, 2005 6:35 pm
by Chris Corbyn
mudvein wrote:can anyone help?? thanks
Yep :D

You want to replace the entire line or just the variable name?

The latter is easy... hint... str_replace()

The former may invlove regex depending upon your intentions.

Please be a bit more specific :)

Posted: Tue Jun 21, 2005 7:24 am
by mudvein
yeah, i meant the entire line, but i've figured it out. there was actually someone who came across the same issue and posted a nice little method on php.net in the file section. thanks again :)

Posted: Sat Dec 31, 2005 2:37 pm
by danf_1979
Do you have the link?
Thanks.