Hi all, if you were trying to block all email addresses from being posted, except for ones that go to a certain domain, how would you do it please? This is what I’ve managed so far:

if ($user->data['user_posts'] < 100000) {
    if (preg_match('/(@|\(at\))/i', request_var('message', '', true))) {
        $error[] = $user->lang['NO_EMAILS'];
    }
}

The helps always appreciated.

    Wouldn't it just be a third if test to see if the address is not in the excepted domain?

      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
      

        PS: The idea being that you then just call that function with the message text, and determine what to do if it returns true (found a disallowed email) or false.

        if ($user->data['user_posts'] < 100000 and check_for_emails(request_var('message', '', true)) {
          $error[] = $user->lang['NO_EMAILS'];
        }
        
          Write a Reply...