What are some common methods for extracting specific lines from a text file in PHP?

When working with text files in PHP, you may need to extract specific lines based on certain criteria. One common method to achieve this is by reading the file line by line and checking each line against the desired criteria. If a line meets the criteria, you can store it in an array or output it directly. Another approach is to use regular expressions to match specific patterns within the lines of the file.

// Open the text file for reading
$file = fopen('example.txt', 'r');

// Loop through each line in the file
while (!feof($file)) {
    $line = fgets($file);
    
    // Check if the line meets the criteria (e.g., contains a specific keyword)
    if (strpos($line, 'keyword') !== false) {
        // Output the line or store it in an array
        echo $line;
    }
}

// Close the file
fclose($file);