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

  • SEARCH
  • Home
  • 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 8482073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:43:51+00:00 2026-06-10T19:43:51+00:00

I am trying to replace multiple parts of a string in a file with

  • 0

I am trying to replace multiple parts of a string in a file with file_put_contents.
Essentially what the function does is finds a particular phrase in the file (which are in the $new and $old arrays and replaces it.

$file_path = "hello.txt";
$file_string = file_get_contents($file_path);
function replace_string_in_file($replace_old, $replace_new) {
    global $file_string; global $file_path;
    if(is_array($replace_old)) {
        for($i = 0; $i < count($replace_old); $i++) {
            $replace = str_replace($replace_old[$i], $replace_new[$i], $file_string);
            file_put_contents($file_path, $replace); // overwrite
        }
    }
}
$old = array("hello8", "hello9"); // what to look for
$new = array("hello0", "hello3"); // what to replace with
replace_string_in_file($old, $new);

hello.txt is: hello8 hello1 hello2 hello9

Unfortunately it outputs: hello8 hello1 hello2 hello3

So it outputs only 1 change when it should have outputted 2:
hello0 hello1 hello2 hello3

  • 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-06-10T19:43:52+00:00Added an answer on June 10, 2026 at 7:43 pm

    That’s a single file, so why output it after every replacement? Your workflow should be

    a) read in file
    b) do all replacements
    c) write out modified file
    

    In other words, move your file_put_contents() to OUTSIDE your loop.

    As well, str_replace will accept arrays for its “todo” and “replacewith” arrays. There’s no need to loop over your inputs. so basically you should have

    $old = array(...);
    $new = array(...);
    
    $text = file_get_contents(...);
    $modified = str_replace($old, $new, $text);
    file_put_contents($modified, ....);
    

    Your main problem is that your str_replace, as you wrote it, is never using the updated string. You constantly use the same ORIGINAL string for each replacement,

    $replace = str_replace($replace_old[$i], $replace_new[$i], $file_string); 
                                                                ^^^^^^^^^^^---should be $replace
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to replace multiple values in a string with JS replace(). The
I am trying to replace a character in a string with multiple occurences in
I am trying to replace multiple string values at once from one variable and
I'm trying to use jQuery to replace all occurrences of a particular string that
I'm trying to replace multiple words with new words, each time I'm rendering an
I'm trying to replace multiple spaces with a single space. When I use ereg_replace
I am trying to replace a large string in groovy. But can't get it
I have been trying to replace a word in a text file with a
I am new to Mercurial and trying to figure out if it could replace
Im trying to replace the following string in php: 2011.11.12 20:06,Teal'c Ostus,Solid Pyroxeres 46521,Pyroxeres

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.