Link to home
Start Free TrialLog in
Avatar of EMB01
EMB01Flag for United States of America

asked on

Add Additional Function to Variable

The code attached takes an array of emails and makes them all lower case. I would also like to validate these emails. How do I change the script below to not only turn them to lower case, but also check if they don't have any characters like "'" or any other stuff that wouldn't be in an email?
// NORMALIZE TO LOWER CASE BECAUSE you@your.org == You@YOUR.OrG IN EMAIL
$existing_emails[] = trim(strtolower($row["email_cre"]));

Open in new window

Avatar of spoxox
spoxox
Flag of Canada image

To validate email addresses (which I believe is the problem here!), you can use regular expressions. Google will find several approaches (see http://www.google.ca/search?q=php+regular+expression+email+address&sourceid=navclient-ff&ie=UTF-8&rlz=1B3GGGL_enCA223CA224).

One solution, using an array of candidate emails as shown in the question is shown in the snippet. It will
  1. write each email address (candidate) from the array $existing_emails
  2. append "ok" or "NO" to indicate whether it's valid.
Note that the eregi PHP function is case insensitive, so the strtolower function can be skipped.




foreach ($existing_emails as $email) {
  echo $email . " ";
  if (eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$', $email)) {
    echo "ok\n";
  }
  else {
    echo "NO\n";
  }
}

Open in new window

Avatar of EMB01

ASKER

Can't I do something simpler like:
// NORMALIZE TO LOWER CASE BECAUSE you@your.org == You@YOUR.OrG IN EMAIL
$existing_emails[] = eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$', trim(strtolower($row["email_cre"])));
// WOULD THIS WORK OR..?

Open in new window

Avatar of EMB01

ASKER

Wait, let me raise the points.

Better yet, I would like to simply remove the special characters (not found in email addresses) like exclamation points, etc. from the array. So, do you have something that I can simply append to the current lower-case'ing script that will just remove specified special characters?
Avatar of EMB01

ASKER

Points raised to 250 from 125.
Before proceeding, one cautionary question:

Is the goal to get a list of valid email addresses from a list that may include some garbage?

If you have something like emb!@you!r.org, you get a valid email address by extracting the ! characters. However,
input like this: !#@$@#!$asdfas)*&d12 will not generate a valid email address. (There's no guarantee that you will end up with a valid email address.) Is it really satisfactory to turn !#@$@#!$asdfas)*&d12 into @@asdfasd12?
Avatar of EMB01

ASKER

Thanks for your concern. The application is only for a list of email addresses like johndoe@hiswebsite.com; but occasionally they're will be an email like johno'doe@hiswebsite.com, know what I mean? So, I just think to remove the apostrophe in such an event would be closer to an actual address.
The attached snippet extracts all characters other than
a-z
A-Z
0-9
@
.
_

Others can be added to $pattern if needed.

The checked/updated email addresses are placed in the $checked_emails array.

$pattern = '[^a-z@\.0-9_]'; // "^" means characters NOT in this list
$replacement = '';
$checked_emails = array();
 
//fill array with each checked and possibly updated $existing_emails value
//note: eregi... the "i" means case-insensitive
foreach ($existing_emails as $email) {
  $checked_emails[] = eregi_replace($pattern, $replacement, trim($email));
}

Open in new window

NB trim($email) above can be replaced with $email as spaces will be extracted.
i.e.
  $checked_emails[] = eregi_replace($pattern, $replacement, trim($email));

can be
  $checked_emails[] = eregi_replace($pattern, $replacement, $email);
Is this working for you? Need clarification?
Avatar of EMB01

ASKER

Thanks, I was just wondering about the trim function from:
$checked_emails[] = eregi_replace($pattern, $replacement, trim($email));

Doesn't it need to be like this to make it lower-case:
trim(strtolower($row["email_cre"]));

Would I do this:
$checked_emails[] = eregi_replace($pattern, $replacement, trim(strtolower($row["email_cre"])));;
ASKER CERTIFIED SOLUTION
Avatar of spoxox
spoxox
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EMB01

ASKER

Thanks for being so thorough! It helps me understand things better. Here's some points! Have a good day.
Avatar of EMB01

ASKER

Thanks, again. Have a good day.
You're welcome!
Avatar of EMB01

ASKER

Hey, quick question: I tried applying the code (as attached) but when I run an email like this:
!!f!@!f!!!.com

I simply submits this:
!!f!@!f!!!.com

I wanted it to be stripped of special characters (such as "!") and submit as this:
f@f.com

Can you help or should a post a new question?
		// added 
		$pattern = '[^a-z@\.0-9_]';
		$replacement = '';
		// end added
		while ($row = mysql_fetch_assoc($rs))
		{
		// NORMALIZE TO LOWER CASE BECAUSE you@your.org == You@YOUR.OrG IN EMAIL
   			// $existing_emails[] = trim(strtolower($row["email_cre"])); <--- changed from this
			$existing_emails[] = ereg_replace($pattern, $replacement, strtolower($row["email_cre"]));
		}

Open in new window