Google Answers Logo
View Question
 
Q: Remove duplicate emails - PHP ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: Remove duplicate emails - PHP
Category: Computers > Programming
Asked by: vladuzca-ga
List Price: $10.00
Posted: 06 Sep 2005 19:19 PDT
Expires: 06 Oct 2005 19:19 PDT
Question ID: 565046
I need a script written in PHP to remove duplicate email
(text@text.text) from a variable. Example script:
$emails = "email1@domain.com email2@domain.com email1@domain.com";
echo remove_duplicates($emails); // should show email1@domain.com only once

Thanks

Clarification of Question by vladuzca-ga on 06 Sep 2005 19:34 PDT
I'd also like to note that delimiters are random (can be a space, long
text or just a line feed)
Answer  
Subject: Re: Remove duplicate emails - PHP
Answered By: palitoy-ga on 07 Sep 2005 03:09 PDT
Rated:5 out of 5 stars
 
Hello 

Thank-you for your question.

The following script should do what you require.  It uses a regular
expression to match email addresses in a string.  These email
addresses are placed in an array from which duplicates are then
removed.

=====================================
$emails = "email1@domain.com email2@domain.com email1@domain.com
email1@domain.com email3@test.co.uk";
print $emails."<br />";
print remove_duplicates($emails);
 
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 .= " ".$value;
  }
  return $str;
};
================================================

If you require any further assistance on this subject please ask for
clarification and I will do my best to respond swiftly.
vladuzca-ga rated this answer:5 out of 5 stars and gave an additional tip of: $2.00
Quick answer and he/she took the time to comment the script!

Comments  
Subject: Re: Remove duplicate emails - PHP
From: palitoy-ga on 07 Sep 2005 03:53 PDT
 
Thank-you for the rating, kind comments and tip - they are all appreciated.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy