How can PHP developers efficiently determine if a specific keyword exists in a text file before extracting specific lines?

To efficiently determine if a specific keyword exists in a text file before extracting specific lines, PHP developers can read the file line by line and use a conditional statement to check if the keyword is present. If the keyword is found, then the specific lines can be extracted and processed accordingly.

$keyword = "example";
$file = fopen("textfile.txt", "r");

if ($file) {
    while (($line = fgets($file)) !== false) {
        if (strpos($line, $keyword) !== false) {
            // Keyword found, extract specific lines
            // Process the specific lines here
        }
    }
    fclose($file);
} else {
    echo "Error opening the file.";
}