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);
?>
Keywords
Related Questions
- How can isset() be used to check for the existence of a variable in PHP?
- What are some recommended methods for optimizing PHP scripts that handle large amounts of XML data for improved performance?
- How can the user modify the PHP code to ensure that all selected .mp3 files are successfully uploaded?