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.
file()function, and some more sensible variable names.