How can the file() function be leveraged in conjunction with preg_match() and a loop to process text data line by line in PHP?

The file() function can be used to read a file into an array line by line in PHP. By combining file() with preg_match() and a loop, you can process text data line by line to search for specific patterns or perform operations on each line.

$file = file('data.txt');

foreach ($file as $line) {
    // Perform operations on each line using preg_match()
    if (preg_match('/pattern/', $line)) {
        // Do something with the line
    }
}