Note that there are some explanatory texts on larger screens.

plurals
  1. POReplace path string in a config file using PHP
    primarykey
    data
    text
    <p>this is a continuation of the previous post. I need to change some parameters(strings) in my config file (.cfg)</p> <p>I have followed Moen's style of coding and was successful in replacing the string which I wanted to replace. </p> <p>Moen's code:</p> <pre><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)); </code></pre> <p>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. </p> <p>foreach($file_data as $line) { list($key, $value) = explode('=', $line); </p> <pre><code>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)); </code></pre> <p>For example if the value of the parameter </p> <pre><code>resource=&lt;?AJIZT?&gt;\srcpdf\srcdef.xml name=timmy file=timmy.txt </code></pre> <p>I am able to change these parameters and expecting output to be</p> <pre><code>resource=htp://localhost/mydocs/doc.pdf name=john file=john.txt </code></pre> <p>But my output looks like this:</p> <pre><code>resource=htp://localhost/mydocs/doc.pdf resource=&lt;?AJIZT?&gt;\srcpdf\srcdef.xml name=john name=timmy file=john.txt file=timmy.txt </code></pre> <p>How can I remove the duplicates? Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload