How can preg_match be used to extract data from text files in PHP?

To extract data from text files in PHP using preg_match, you can create a regular expression pattern that matches the specific data you want to extract. Then, use preg_match function to search the text file using the pattern and retrieve the matched data.

$pattern = '/(regex_pattern_here)/'; // Define your regex pattern
$file_content = file_get_contents('example.txt'); // Read the text file
if (preg_match($pattern, $file_content, $matches)) {
    $extracted_data = $matches[0]; // Extract the matched data
    echo $extracted_data; // Output the extracted data
} else {
    echo 'No match found'; // Handle case where no match is found
}