Replace Line in a file?

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
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Replace Line in a file?

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Replace Line in a file?

Post 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 :)
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post 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 :)
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post by danf_1979 »

Do you have the link?
Thanks.
Post Reply