This is what I came up with, FWIW:
<?php
function check_for_emails($message) {
$regex = '/\b\w+(@|\s?\(at\)\s?)(\w+(\.\w+)+)\b/i';
if (preg_match_all($regex, $message, $matches)) {
foreach($matches[2] as $match) {
if(strtolower($match) != 'ok.com') {
return true;
}
}
}
return false;
}
/* TEST */
$tests = [
"This is a test@foo.com. It is only a test.name@bar.com.uk or test(at)foo.com, and don't forget this.is@ok.com.",
"This is a test.is@ok.com that should be fine (at) ok.com, I hope."
];
foreach($tests as $test) {
echo "$test\n ...is ";
if(check_for_emails($test)) {
echo "NOT OK\n";
}
else {
echo " OK\n";
}
}
Result:
This is a test@foo.com. It is only a test.name@bar.com.uk or test(at)foo.com, and don't forget this.is@ok.com.
...is NOT OK
This is a test.is@ok.com that should be fine (at) ok.com, I hope.
...is OK