Okay, here's what I'm attempting to do. I am trying to make a script that displays a form to fill out configuration vars, and when posted it will open a template of the configuation file (with comments and all) that has the areas to be replaced marked with %%var1%% %%var1_data%% . When it's done, it writes it out to a final configuration file.
I'm doing it this way so that if the configuration changes in the future, it is easier to just update the config template than to put the entire config file in the script and parse it out.
I've gotten it boiled down to the script below (after much head banging and searching through mailing lists, forums, websites, and under my bed). It does mostly what I want, except it prints out the whole config for each var it parses with str_replace. The final iteration is what I want, with all vars replaced and comments intact. Help! Please! Email me if you have a solution.
Script:
<?
$fw = fopen ("test.conf","w") or die ("Can't write file");
$file = file('conf.inc');
while (list ($var, $val) = each ($HTTP_POST_VARS)) {
$foo = array("%%$var%%", "%%$var"."_data%%");
$bar = array("\$$var",$val);
for ($y = 0; $y < sizeof($file); $y++) {
$file[$y] = str_replace ($foo, $bar,$file[$y]);
fwrite($fw, stripslashes($file[$y]));
}
}
fclose($fw);
?>
<form method="post" action="<?=$PHP_SELF?>">
<input type="text" name="var1" />
<input type="text" name="var2" />
<input type="text" name="var3" />
<input type="text" name="var4" />
<input type="submit" value="smash this" />
</form>
Config Template:
%%var1%% = "%%var1_data%%";
this is some comments
//these are some more
%%var2%% = "%%var2_data%%";
%%var3%% = "%%var3_data%%";
%%var4%% = "%%var4_data%%";
I hope someone can give me a clue, I feel so drained drying to figure this out 🙂
Edward Ritter