How can PHP be used to write extracted email addresses from a text file into a new file efficiently and effectively?

To efficiently and effectively write extracted email addresses from a text file into a new file using PHP, we can read the contents of the text file, extract email addresses using regular expressions, and then write these email addresses into a new file.

<?php
// Read the contents of the text file
$text = file_get_contents('input.txt');

// Extract email addresses using regular expression
preg_match_all('/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/', $text, $matches);

// Write extracted email addresses into a new file
$emails = implode("\n", $matches[0]);
file_put_contents('output.txt', $emails);
?>