How can memory consumption be managed when using functions like strpos() and strrchr() in PHP to manipulate flatfile data?

When using functions like strpos() and strrchr() in PHP to manipulate flatfile data, memory consumption can be managed by reading the file line by line rather than loading the entire file into memory at once. This can be achieved by using functions like fgets() or fopen() in conjunction with the aforementioned functions to search for specific data within the file without loading the entire contents into memory.

$file = fopen('data.txt', 'r');

if ($file) {
    while (($line = fgets($file)) !== false) {
        $pos = strpos($line, 'search_string');
        // Perform desired operations with the found position
    }

    fclose($file);
}