What are the best practices for searching for keywords within files in a PHP application?

When searching for keywords within files in a PHP application, it is important to use the appropriate functions and methods to efficiently search through the contents of the files. One common approach is to use PHP's file handling functions to read the contents of the file line by line and then search for the desired keyword within each line.

<?php
// Specify the keyword to search for
$keyword = 'example';

// Specify the file to search within
$file = 'file.txt';

// Open the file for reading
$handle = fopen($file, 'r');

// Loop through each line in the file
while (($line = fgets($handle)) !== false) {
    // Check if the keyword is present in the current line
    if (strpos($line, $keyword) !== false) {
        echo "Keyword found in file: $file\n";
        break;
    }
}

// Close the file
fclose($handle);
?>