Hi there sneaky,
You're absolutely right that you've provided 100% of the working code. I could show you my attempts at both doing it myself and at modifying your offering, but they're so pathetic, that I thought I would leave them out.
I have run the code. The reason I thought it wasn't catching the duplicates is because thousands of server.com addresses were getting saved, but at closer look, the part before the @ is a little different each time.
Here is the code as I'm trying to use it right now. It saves all email addresses(barring dupes) to the file, so it's working as it should in that regard.
<?php
$file = "C:\\Program Files\\xampp\\htdocs\\entries.txt";
echo $file."<br>";
$contents = file_get_contents($file);
echo $contents."<br>";
if ($contents === FALSE) {
die('could not fetch file contents');
}
$pattern = '/(\w+\.)*\w+@(\w+\.)*\w+(\w+\-\w+)*\.\w+/';
$num_matches = preg_match_all($pattern, $contents, $matches);
if ($num_matches > 0) {
$addresses = array();
foreach($matches[0] as $m) {
if (!in_array($m, $addresses)) {
$addresses[] = $m;
}
}
} else {
echo "no matches\n";
}
// put the results in this file
$dest = 'C:\\Program Files\\xampp\\htdocs\\output.txt';
if (!file_put_contents($dest, implode(",\r\n", $addresses))) {
die("unable to write destination file\n");
} else {
echo "everything seems ok, please check file $dest\n";
}
?>
I'm editing this as I go, so forgive me if you're reading something different now 🙂
I've managed to get it to save only the emails after "Email: ". The problem I'm having now is that it's saving "Email: " with the addresses 🙂
$pattern = '/Email: (\w+\.)*\w+@(\w+\.)*\w+(\w+\-\w+)*\.\w+/';
Any help on getting it to dump that portion after it finds it would be very welcome. I'm going back to my tutorial now 🙂
I know I could get rid of the "Email: " with my text editor, but I feel I owe it to myself that I figure out how to do this. I am completely incompetent at regex.
thanks,
json