_Unique_ Posted May 17, 2015 Share Posted May 17, 2015 Hello, I am trying to replace strings in another php file. The strings I am trying to change determine the database connection (the ip, user, etc). I am using str_replace() however, when the functions process they wipe the whole file leaving nothing inside it, whereas there was the strings, etc there before. This is my code that is supposed the change the strings: $connectFile = $_SERVER['DOCUMENT_ROOT'].'/config/connect.php'; $connectContents = file_get_contents($connectFile); $connectContents = str_replace('', $_SESSION['dbHost'], $host); $connectContents = str_replace('', $_SESSION['dbUser'], $user); $connectContents = str_replace('', $_SESSION['dbPass'], $pass); $connectContents = str_replace('', $_SESSION['dbName'], $db); file_put_contents($connectFile, $connectContents); echo 'Install Finished!'; And this is my connect.php, the file which contains the strings that needs to be changed; <?php $host = ''; $user = ''; $pass = ''; $db = ''; $dbc = mysqli_connect($host, $user, $pass, $db) or errorDie('Cannot connect to database!', 'Unfortuantly, there was an error with the connection to the database. If you are the site admin, please try and resolve this issue. Sorry for any inconvenience.'); ?> Thanks in advance, Unique Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 17, 2015 Share Posted May 17, 2015 When using a function, you need to understand what the parameters are what they are for. The str_replace function takes 1) the search string, 2) the replacement string and 3) the subject (i.e. the content which you want to replace the strings in). Your code makes no use of the subject. What was wrong with the code I provided you on the previous topic to do the same thing? You can't use str_replace() to replace the value of a line like $host = ''; because there are multiple instances of = ''. You need to use a regular expression to find and replace the values for those variables. Or, you have to search and replace the entire line (e.g. search for "$host = '';"). But, doing that limits you to only replacing the values if they have never been set before. That is why I provided a regex solution that can be used to set the initial value as well as updating the values in the future. If you are not going to use the help provided then some people (i.e. me) will likely stop helping you. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 17, 2015 Share Posted May 17, 2015 if you are at the point of trying to make an install script, all your code manipulating the values should be general purpose, rather than hard-coded with specific variable/defined constant names. if you are using variables, use an array ($config['db_host']). if you are using defined constants, use a prefix on the names (CONFIG_DB_HOST) so that you can distinguish them from any other defined constants you have in your code. the only thing in your config file should be the settings. there should be no functional code, such as the database connection. then, at the point of saving the values, why not just loop over the array/prefixed named defined constants and write out the php code containing the current values? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.