Basically I want PHP to open a config file, search for a string and replace what comes after
The problem that I have in the code that I have created is that it finds the string $db_pass = and is able to replace it… but then in the file there is an extra "password"); line… so i need it to be able to replace the entire line or either cut the rest to be able to delete it.
$dbFile = 'dbconfig.php';
$String = "\$db_pass =\"new_password\";\n";
file_put_contents($dbFile, str_replace("\$db_pass =", $String, file_get_contents($dbFile)));
dbconfig.php
<?php
// Database Constants
db_pass = "hi";
db_user = "hssi";
?>
my current script outputs like this
dbconfig.php
<?php
// Database Constants
db_pass = "new_password";
"hi";
db_user = "hssi";
?>
You want to use a regular expression to replace the whole line, rather than the beginning of the line.
preg_replace()is the PHP function for find and replacing with regular expressions.Sample code to do what you’re looking for would be: