EMB01
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"]));
Of course, EE has solutions as well:
For a list, see
https://www.experts-exchange.com/simpleSearch.jsp?q=php+validate+email&searchSubmit=1&cid=315&redirectURL=%2F&gSearch=0
For one candidate solution, see
https://www.experts-exchange.com/questions/21742294/php-validate-email.html?sfQueryTermInfo=1+email+php+valid
For a list, see
https://www.experts-exchange.com/simpleSearch.jsp?q=php+validate+email&searchSubmit=1&cid=315&redirectURL=%2F&gSearch=0
For one candidate solution, see
https://www.experts-exchange.com/questions/21742294/php-validate-email.html?sfQueryTermInfo=1+email+php+valid
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..?
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?
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?
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?
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?
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.
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));
}
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);
$checked_emails[] = eregi_replace($pattern, $replacement, trim($email));
can be
$checked_emails[] = eregi_replace($pattern, $replacement, $email);
Is this working for you? Need clarification?
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["emai l_cre"]));
Would I do this:
$checked_emails[] = eregi_replace($pattern, $replacement, trim(strtolower($row["emai l_cre"]))) ;;
$checked_emails[] = eregi_replace($pattern, $replacement, trim($email));
Doesn't it need to be like this to make it lower-case:
trim(strtolower($row["emai
Would I do this:
$checked_emails[] = eregi_replace($pattern, $replacement, trim(strtolower($row["emai
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for being so thorough! It helps me understand things better. Here's some points! Have a good day.
ASKER
Thanks, again. Have a good day.
You're welcome!
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?
!!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"]));
}
One solution, using an array of candidate emails as shown in the question is shown in the snippet. It will
Open in new window