Jump to content

Replace A Line In Text File


dakke

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...