0

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

125369
  • 3,547
  • 20
  • 52
  • 70
  • IMO fwrite is no bad idea, you should pursure that and find out what the problem was. With statements like 'was not sucessful' you won't find any help! – Anonymous Nov 23 '11 at 13:26
  • I guess the problem is that Raaks wants to have the like that begins with "resource=" only once. right? – belgther Nov 23 '11 at 13:32
  • Hi raaks : http://stackoverflow.com/questions/2159059/string-replace-in-a-large-file-with-php this will solve your problem – vikas Nov 23 '11 at 13:34

1 Answers1

0

If your config file is not too big you can use file_get_contents and file_puts_contents. It allow you to manipulate your file as a big string.

$url = 'htp://localhost/mydocs/doc.pdf';

$file='config.cfg';
$file_data = explode("\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("\n",$res_data));

Warning, check your end-of-line delimiters:

explode("\n",file_get_contents($file));

should be

explode("\r\n",file_get_contents($file));

If you are familiar with it, preg_replace can do the job well.

Moen
  • 36
  • 3
  • Hi Moen,thanks a lot for your piece of code it works perfectly fine. I have just one more question to add to this. What if I want to replace multiple strings instead of one, like resource, name, id, etc etc – 125369 Nov 23 '11 at 14:47