pdo - Replace full line of PHP in another file -
i update database configuration file dynamically. first, copy theme files on 1 directory directory. these theme files contain database configuration file.
after files copied over, update database configuration file use name of new database created (using $dbname).
$dbname = "database 1"; $data = file('file.php'); // reads array of lines function replace_a_line($data) { if (stristr($data, 'dbname=')) { return 'dbname=' . $dbname . ''; } return $data; } $data = array_map('replace_a_line',$data); file_put_contents('file.php', implode('', $data)); file.php:
$this->pdo = new pdo('mysql:host=localhost; dbname=', '', ''); my problem function above replaces entire line just
dbname= i don't know how use return proper syntax add full line of php
i need this:
$this->pdo = new pdo('mysql:host=localhost; dbname=test', '', ''); what can use retain original php line , add dbname= ?
what if replaced line entire file contained above line of php way can insert whole php line?
here corrections in code make works expect:
$data = file('file.php'); // reads array of lines function replace_a_line($data) { $dbname = "database 1"; if (stristr($data, 'dbname=')) { return str_replace('dbname=', 'dbname=' . $dbname, $data); } return $data; } $data = array_map('replace_a_line',$data); file_put_contents('file.php', implode('', $data));
Comments
Post a Comment