How can a PHP script search through a text file for specific data efficiently?

To efficiently search through a text file for specific data in PHP, you can use the `file()` function to read the file into an array line by line, and then use a loop to search for the desired data. You can also use regular expressions to match patterns in the text file.

<?php
// Open the text file
$lines = file('data.txt');

// Specify the data you are searching for
$searchTerm = 'specific data';

// Loop through each line in the file
foreach ($lines as $line_num => $line) {
    // Check if the line contains the search term
    if (strpos($line, $searchTerm) !== false) {
        echo "Found '$searchTerm' in line $line_num: $line\n";
    }
}
?>