I have a PHP configuration file which stores settings like:
define("SETTING_1", "");
define("SETTING_2", "");
define("SETTING_3", "");
By default the settings are empty, but then I have an installer file install.php which basically prompts the user for the settings. What is the best way to open the settings file, find the define for each setting and insert the value? Finally, need to save the settings file to disk.
I want to ovoid using regular expressions or simple string search. I came across token_get_all(), does it makes sense to use it?
token_get_all()isn’t really going to help you, since it’s just going to give you all of the parse token from the string. You’ll spend more time writing a state machine or parser for the result oftoken_get_all()than you would with the other methods you listed.The easiest way to achieve this is to use some sort of known, unique placeholder, like this:
Then, you open the file, and replace all of those placeholders with their actual values using
str_replace():That’s all it takes.