In PHP, what methods can be used to handle and process large text files with multiple instances of the same keyword, like "Host"?

When handling large text files with multiple instances of the same keyword like "Host", one approach is to read the file line by line and use regular expressions to search for the keyword. By using the preg_match_all function, we can extract all occurrences of the keyword from each line and process them accordingly.

$keyword = 'Host';
$file = 'large_text_file.txt';

$handle = fopen($file, 'r');
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        preg_match_all("/\b$keyword\b/", $line, $matches);
        
        foreach ($matches[0] as $match) {
            // Process each occurrence of the keyword
            echo $match . "\n";
        }
    }

    fclose($handle);
} else {
    echo "Error opening the file.";
}