How can regular expressions be utilized in PHP to extract specific text patterns from a file?

Regular expressions can be utilized in PHP to extract specific text patterns from a file by using functions like preg_match() or preg_match_all(). These functions allow you to define a pattern using regular expressions and then search for matches within the file content. By specifying the desired pattern, you can extract specific text that follows that pattern from the file.

$file_content = file_get_contents('example.txt'); // Read the content of the file
$pattern = '/[0-9]{3}-[0-9]{3}-[0-9]{4}/'; // Define the pattern to match phone numbers
preg_match_all($pattern, $file_content, $matches); // Search for all phone numbers in the file content

foreach ($matches[0] as $match) {
    echo $match . "\n"; // Output each phone number found in the file
}