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 6752911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:05:58+00:00 2026-05-26T13:05:58+00:00

This is my code: $string2 = file_get_contents(‘maps/’ . $region . ‘.txt’); $string2 = explode(\n,

  • 0

This is my code:

$string2 = file_get_contents('maps/' . $region . '.txt');
$string2 = explode("\n", $string2);
foreach($string2 as $value2) {
   $string2 = unserialize($value2);
   if($string2['x_pos'] == ($x2 + 4) && $string2['y_pos'] == ($y2 + 8)) {
      $length2 = strlen($string2['amount']);
      $new_amount = ($string2['amount'] + 0) - ($resource_quantity + 0);
      $changed = substr_replace($value2, $new_amount, 123, $length2);
      file_put_contents('maps/' . $region . '.txt', $changed);
      break 1;
   }
}

What I want the code to do is to open the file, read each line until it finds the line it wants and then re-save the file with the edited line. The problem is, that it works, but it only saves it with the edited line, and gets rid of all of the other lines.

I want to keep with the method I’ve used (file_get_contents & file_put_contents) really, unless there is an incredibly easier way to do it. Can someone please help? I’ve been searching for some time and cannot find what I’m looking for.

  • 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-26T13:05:59+00:00Added an answer on May 26, 2026 at 1:05 pm

    You need to move the write operation after the loop and have it write everything you read from the file. The way you currently have it, it’s replacing all the contents with just $changed (which is just one line).

    The above, in addition to improving the code a bit, leads us to:

    $filename = 'maps/' . $region . '.txt';
    $lines = file($filename);
    foreach($lines as &$line) { // attention: $line is a reference
        $obj = unserialize($line);
        if(/* $obj satisfies your criteria*/) {
            $line = /* modify as you need */;
            break;
        }
    }
    
    file_put_contents($filename, implode("\n", $lines));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this code is correct?? String note = text.txt; FileWriter file = new FileWriter(note); Scanner
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
In my PHP code I have this: $filename = 'data.xml'; $xml = file_get_contents($filename); $data
This code enables to create a new .txt file, if the file doesn't exist
<?php $html = file_get_contents('http://hypermedia.ids-mannheim.de/'); ?> this code returns me the html of the website
I have this code in PHP: <?php $data = file_get_contents('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/rss.xml'); $xml = simplexml_load_string($data); echo
This is my code: File TempFiles = new File(Tempfilepath); if (TempFiles.exists()) { String[] child
Suppose I have this code: String encoding = UTF-16; String text = [Hello StackOverflow];
I have written this code to convert string in such format 0(532) 222 22
Just curious how you would comment this line of code: string customerNm = customerNm.EndsWith(s)

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.