this is a continuation of the previous post. I need to change some parameters(strings) in my config file (.cfg)
I have followed Moen's style of coding and was successful in replacing the string which I wanted to replace.
Moen's code:
$url = 'htp://localhost/mydocs/doc.pdf';
$file='config.cfg';
$file_data = explode("\r\n",file_get_contents($file));
foreach($file_data as $line) {
list($key, $value) = explode('=', $line);
if($key == "resource") {
$res_data[] = "$key=$url\n";
continue;
}
$res_data[] = $line;
}
file_put_contents("config.cfg", join("\r\n",$res_data));
But on the other hand I need to replace not one string but multiple strings like resource, name, file etc etc. I tried to use a switch statement and look for all the strings I want to replace. I can replace them all but the problem is I am creating a copy of the changed strings with the original parameters in tact.
foreach($file_data as $line) { list($key, $value) = explode('=', $line);
switch($key){
case "resource":
$res_data[] = "$key=$url";
break;
case "name":
$res_data[] = "$key=$name";
break;
case "file":
$res_data[] = "$key=$file";
break;
}
$res_data[] = $line;
}
file_put_contents("config.cfg", join("\r\n",$res_data));
For example if the value of the parameter
resource=<?AJIZT?>\srcpdf\srcdef.xml
name=timmy
file=timmy.txt
I am able to change these parameters and expecting output to be
resource=htp://localhost/mydocs/doc.pdf
name=john
file=john.txt
But my output looks like this:
resource=htp://localhost/mydocs/doc.pdf
resource=<?AJIZT?>\srcpdf\srcdef.xml
name=john
name=timmy
file=john.txt
file=timmy.txt
How can I remove the duplicates? Thanks