Removing duplicarte email addresses from existing list
4 posts by 2 authors in: Forums > CMS Builder
Last Post: December 21, 2012 (RSS)
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
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Is there a particular reason that your using output buffering to record the e-mail addresses into the array? I've modified your code so that it stores the e-mail addresses directly into an array:
<?php
$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('; ', $output);
//... and then display it!
echo $final_object;
?>
So in this example I'm storing the e-mail address directly into the array, but only if the address isn't already in there.
Let me know if this doesn't work, or if you need something different.
Thanks!
Greg
PHP Programmer - interactivetools.com
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
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;
?>
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php