Unique email addresses by domain
<p>Im trying to make the below function only return 1 email per domain. </p>

<p>Example: if i feed the function:</p>

<blockquote>
  <p>email1@domain.com email2@domain.com email1@domain.com
  email1@domain.com email3@test.co.uk</p>
</blockquote>

<p>I want it to return </p>

<blockquote>
  <p>email1@domain.com email3@test.co.uk</p>
</blockquote>

<p>Here is the current function:</p>

<pre><code>function remove_duplicates($str) {

  # match all email addresses using a regular expression and store them
  # in an array called $results
  preg_match_all("([\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7})",$str,$results);

  # sort the results alphabetically
  sort($results[0]);

  # remove duplicate results by comparing it to the previous value
  $prev="";
  while(list($key,$val)=each($results[0])) {
    if($val==$prev) unset($results[0][$key]);
    else $prev=$val;
  }

  # process the array and return the remaining email addresses
  $str = "";
  foreach ($results[0] as $value) {
     $str .= "&lt;br /&gt;".$value;
  }

  return $str;
};
</code></pre>

<p>Any ideas how to achieve this?</p>
