Removing duplicarte email addresses from existing list

4 posts by 2 authors in: Forums > CMS Builder
Last Post: December 21, 2012   (RSS)

I’ve been mucking about trying to remove duplicate entries from an existing email list and although I got this to work, it’s really dependent on the lack of spaces spaces in the foreach loop code.

I’m thinking that there must be an easier way to insure that this works.

Here’s the code that I’m using:

<?php ob_start(); // start capturing output ?>
<?php foreach($client_uploadsRecords as $record) : ?><?php foreach ($record['uploads'] as $upload): ?><?php if($upload['info5'] == 0 || !$upload['info5']) : ?><?php $email = strtolower($record['email']) ; ?>
<?php // Replace all errant spaces in $email field with nothing
$email = preg_replace("/[ ]/", "", $email); ?><?php echo $email ?>;<?php endif; ?><?php endforeach ?><?php endforeach ?>
<?php $output = ob_get_clean(); // stop capturing output ?>
<?php // Replace all spaces in $output with nothing
$object = preg_replace("/[ ]/", "", $object); ?>
<?php $output = implode('; ',array_unique(explode(';', $output))); ?>
<?php echo $output ?>


Thanks for the help,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Hi Greg,

I've been out of the loop for a few dayys. Thanks for responding. I would have answered earlier but with the new forum format, I never got an email that you responded.

No, there was no reason for using output buffering. I just couldn't think of another way.

Maybe this will solve the space issue.

I'll try it and post back.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - December 21, 2012 - edited: December 21, 2012

Hi Greg,

After a few minor tweeks, like adding a close parenthesis after foreach ($record['uploads'] as $upload and changing $ouput to $object in final_object = implode('; ', $output); it worked like a charm with no more space issues.

Final code is below.

Thanks again,

BTW, Thanks for annotating the code so that I could understansd what was going on.

Jerry Kornbluth

$object = array();
foreach($client_uploadsRecords as $record){
foreach ($record['uploads'] as $upload){
if($upload['info5'] == 0 || !$upload['info5'])
{
//change e-mail address to lowercase.
$email = strtolower($record['email']) ;
//clean e-mail address
$email = preg_replace("/[ ]/", "", $email);
//If the e-mail address isn't already in the array, add it too it.
if(!in_array($email,$object)){
$object[] = $email;
}
}
}
}
//implode the output array.
$final_object = implode('; ', $object);
//... and then display it!
echo $final_object;
?>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php