We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Add Additional Function to Variable

EMB01
EMB01 asked
on
Medium Priority
280 Views
Last Modified: 2013-12-12
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

Comment
Watch Question

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

Author

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

Author

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?

Author

Commented:
Points raised to 250 from 125.

Commented:
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?

Author

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.

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

Commented:
NB trim($email) above can be replaced with $email as spaces will be extracted.

Commented:
i.e.
  $checked_emails[] = eregi_replace($pattern, $replacement, trim($email));

can be
  $checked_emails[] = eregi_replace($pattern, $replacement, $email);

Commented:
Is this working for you? Need clarification?

Author

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"])));;
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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

Author

Commented:
Thanks, again. Have a good day.

Commented:
You're welcome!

Author

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

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.