I'd like to use php to open a file and replace just one line that will be near the top. Since it's near the top I don't want to read the whole file into memory. Here is what I've tested but I think I am not understanding how to backspace over the line once I identify it using strpos():
<?php
$file = "test-file.php";
$new_value = "colors:orange green violet;";
$fhandle = fopen($file, "r+") or die("Unable to open file!");
$replaced = "false";
while ($replaced === "false") {
$line = fgets($fhandle);
if (strpos($line, "colors:")) { //find the colors line
//Should I be moving the file pointer back here?
//Once I find the line, how do I clear it?
$line = $new_value;
fputs($fhandle, $line);
$replaced = "true";
}
}
fclose($fhandle);
?>
Contents of test-file.php:
fruit:apples bananas;
colors:red blue green;
cars:ford chevy;
Note: Each line in test-file.php ends with a semicolon.