You want to find all the lines in a file that match a pattern.
Read the file into an array and use preg_grep( ).
There are two ways to do this. Example 22-9 is faster, but uses more memory. It uses the file( ) function to put each line of the file into an array and preg_grep( ) to filter out the non-matching lines.
Quickly finding lines that match a pattern
Example 22-10 is slower, but more memory efficient. It reads the file a line at a time and uses preg_match( ) to check each line after it's read.
Efficiently finding lines that match a pattern
Since the code in Example 22-9 reads in everything all at once, it's about three times faster than the code in Example 22-10, which parses the file line by line but uses less memory. Keep in mind that since both methods operate on individual lines of the file, they can't successfully use patterns that match text that spans multiple lines.
22.5.4. See Also
Recipe 23.5 on reading files into strings; documentation on preg_grep( ) at http://www.php.net/preg-grep.