dakke Posted December 29, 2009 Share Posted December 29, 2009 Hi,How can I replace a line in a php file, where e.g. I find $db_connection?So, say that on line 4 I have: $db_connection = 'justanexample'; or even $db_connection = 'justanotherexample'; Is there a way to search for $db_connection and replace the entire line with say $db_connection = 'replaced line'; ?Thanks Link to comment Share on other sites More sharing options...
Redroest Posted December 29, 2009 Share Posted December 29, 2009 Cant you just redeclare the $db_connection variable when php reads the file as a php file? I mean when you do something like:include("Database.php");Database.php <?php$db_connection = "database1";?> index.php <?phpinclude("Database.php");if(isset($db_connection) || $domain == "text.nl")){ $db_connection = "database2";}else{ echo 'Database is set to the default';}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted December 29, 2009 Share Posted December 29, 2009 If you want to replace the part between quotes you need to use a regular expression with preg_replace. You could also use strpos to search for the beginning of the line and then the position of the next newline and replace the entire line with your own, or you could split the lines up into an array and loop through checking for the one you're looking for. Link to comment Share on other sites More sharing options...
dakke Posted December 29, 2009 Author Share Posted December 29, 2009 Thanks, that helped a lot! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.