Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6852569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:23:12+00:00 2026-05-27T01:23:12+00:00

this is a continuation of the previous post. I need to change some parameters(strings)

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:23:13+00:00Added an answer on May 27, 2026 at 1:23 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a continuation question from a previous question I have asked I now
This is a continuation of a previous post: TPanel color is black no matter
This is a continuation from a previous stackoverflow question. I've renamed some variables so
This question is in continuation to my previous post located here . Since there
This post is an continuation of this post I have DlUser Class each object
This is a continuation of a previous question . I have: var X =
This question is in continuation of this post , I have tried installing Xerces-C
This is a continuation of a couple of previous questions I've had. I have
This is sort of a continuation of this question . I have a file
I have some lines like this from an ldiff file, dn: cn=dkalland_directs_ww,cn=org_groups,cn=beehive_groups,cn=groups,dc=oracle ,dc=com businesscategory:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.