• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

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

0
EMB01
Asked:
EMB01
  • 9
  • 8
1 Solution
 
spoxoxCommented:
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

0
 
EMB01Author Commented:
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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
EMB01Author Commented:
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?
0
 
EMB01Author Commented:
Points raised to 250 from 125.
0
 
spoxoxCommented:
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?
0
 
EMB01Author Commented:
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.
0
 
spoxoxCommented:
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

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

can be
  $checked_emails[] = eregi_replace($pattern, $replacement, $email);
0
 
spoxoxCommented:
Is this working for you? Need clarification?
0
 
EMB01Author Commented:
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"])));;
0
 
spoxoxCommented:
RE trim:
The eregi_replace with the pattern as used above will strip out blanks. Trim() becomes redundant.

RE strtolower:
If you want your output email addresses all in lower case (maybe for sorting?), then use strtolower() and ereg_replace() instead of eregi_replace() (see version 1 below). If you don't care how the email addresses look, you can omit strtolower() (see versions 2 and 3 below).




// VERSION 1: output email addresses will be in lower case
// This pattern will match anything that is not one of:
//   * lower case letter (a-z)
//   * @ ("at" sign")
//   * . (dot) ** NOTE: "." is a special regular expression character, escaped with "\"
//   * digit (0-9)
//   * _ (underscore)
  $pattern = '[^a-z@\.0-9_]';
  $replacement = '';
  foreach ($addrs as $email) {
    // ereg_replace does a case-sensitive match of the characters in $email. 
    // Any characters in $email that match any characters in $pattern are replaced with
    // the characters from $replacement.
    $checked_emails[] = ereg_replace($pattern, $replacement, strtolower($email));
  }
 
// VERSION 2: output email addresses will be in original case, perhaps mixed
// This pattern will match anything that is not one of:
//   * letter (a-z) ** NOTE: eregi_replace is case-insensitive: will match A-Z also.
//   * @ ("at" sign")
//   * . (dot)
//   * digit (0-9)
//   * _ (underscore)
  $pattern = '[^a-z@\.0-9_]';
  $replacement = '';
  foreach ($addrs as $email) {
    // eregi_replace does a case-insensitive match of the characters in $email. 
    // Any characters in $email that match any characters in $pattern are replaced with
    // the characters from $replacement.
    $checked_emails[] = eregi_replace($pattern, $replacement, $email);
  }
 
// VERSION 3: output email addresses will be in original case, perhaps mixed.
// This pattern will match anything that is not one of:
//   * lower case letter (a-z)
//   * upper case letter (A-Z)
//   * @ ("at" sign")
//   * . (dot)
//   * digit (0-9)
//   * _ (underscore)
  $pattern = '[^a-zA-Z@\.0-9_]';
  $replacement = '';
  foreach ($addrs as $email) {
    // ereg_replace does a case-sensitive match of the characters in $email. 
    // Any characters in $email that match any characters in $pattern are replaced with
    // the characters from $replacement.
    $checked_emails[] = ereg_replace($pattern, $replacement, $email);
  }

Open in new window

0
 
EMB01Author Commented:
Thanks for being so thorough! It helps me understand things better. Here's some points! Have a good day.
0
 
EMB01Author Commented:
Thanks, again. Have a good day.
0
 
spoxoxCommented:
You're welcome!
0
 
EMB01Author Commented:
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

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now