Regular Expressions - Validating Email Addresses
Yesterday during the wee hours I added a mail list to my form and today I'm going to go through how I did it. The first thing you need to do is determine if the email you are dealing with is in the valid format or not. Validating email addresses isn't a perfect science, unfortunately. When you think of all the different possible formats, you have to be pretty broad -- maybe too broad -- in defining a matching pattern (or regular expression, whatever you prefer to call it). You will find many different regular expressions other programmers have written for validating email addresses so mine certainly isn't the only one (nor am I egotistical enough to suggest it is the best one), but it is one that works:
// join the mail list?
if ($php_script_list == "yes")
{
// is the $from email address in valid format?
if(ereg("([[:alnum:]\.\-]+)(\@[[:alnum:]\.\-]+\.+)", $from))
First we make sure the $php_script_list checkbox is checked.
Then we check the syntax of the user submitted $from email address. The first portion of
the email needs to contain at least one but likely more {alnum} alphanumeric (a-z
or 0-9) character or the dash - symbol then there must be the @ symbol followed by at
least one, but likely more. Then there is more alphanumeric characters and at least one
period mixed in there. That's what that long somewhat cryptic regular expression means.
abc@123.com would be a valid email
abc@123com woud be an INvalid email (missing period)
1-1@1-1.com would be a valid email
1-2.com would be an INvalid email (missing @)
In regular expressions you enclose ranges within brackets. Thus [a-z] would mean any letter between a-z would return true. [a-c] would only return true if a,b,c was in the comparison string. I encourage you to refer to the php manual and look at the ereg function again.Note that when using ereg function patterns are case sensitive.
int ereg
(string pattern, string string, array [regs]);
Use the eregi function in place of ereg for patterns that are case INsensitive. Let's look at the rest of the mail list code:
if(file_exists("email_list.txt"))
{
$newfile = fopen("email_list.txt", "r");
while(!feof($newfile))
{
$duplicate = fgetss($newfile, 255);
// is email address submitted already on file?
if(eregi("$from",
$duplicate))
{
print("<HTML><BODY>This
email <strong>$from</strong>");
print(" is already in the database.
<br>Please go back");
print(" and uncheck the mail list
box</BODY></HTML>");
fclose($newfile);
exit;
}
}
fclose($newfile);
$addemail = fopen("email_list.txt", "a");
fputs($addemail, "$from\n");
fclose($addemail);
}
else
{
// since file doesn't already exist, let's create for first time
$newfile = fopen("email_list.txt", "a");
fputs($newfile, "$from\n");
fclose($newfile);
chmod("email_list.txt", 0666);
}
Start by determining whether the email_list.txt file exists. If it does not exist then this is the very first time someone is being added to the list and we don't need to bother with checking to see whether their email address is in the list or not. So we only need to create the file and write the email address. You may also notice I used the chmod function to set the file permissions for the email_list.txt file. This line of code is optional. When using the "a"ppend file option, Unix will automatically create the file for the first time and set the permissions. The problem is, depending, on the directory and umask settings it may create a file that only the script has owner permissions to read and write and update. By using the chmod function sometimes can be the only way to gain the permissions you want once a script has set the initial permissions. If you try to FTP a file to a directory and get the "permissions denied" error, you may very well need to write a small PHP script using the chmod command to change the permissions of the file (or simply delete the file, but then you'll lose the data in it). Ok, but let's say the email_list.txt file does exist, now the most logical thing to do is make sure we don't already have that same email address on file. We do this by reading a line at a time of the existing file and comparing that line (I named the variable $duplicate) against the $from email address and seeing if there is a match. If there is then we print the message to the browser letting the person submitting the form know that we already have that email address in the database, close the open file, and exit the script. They can go back and uncheck the box and resubmit and the email will go through fine. Now if the email address does not match another email in the database they are appended to the end of the database.
Changing a strings case in PHP
Want to force text in a string to be all capital or lowercase letters? You could do this the long way using regular expressions, but PHP has several cool built-in functions for this.
<?
// force all uppercase
print(strtoupper("i bet this will show up as all letters
capitalized<br>"));
// force all lowercase
print(strtolower("I BET THIS WILL SHOW UP AS ALL LETTERS IN
LOWERCASE<br>"));
// force the first letter of a string to be capitalized
print(ucfirst("i bet this will show the first letter of the string
capitalized<br>"));
// force the first letter of each WORD in a string to be capitalized
print(ucwords("i bet this will show the first letter of every word
capitalized<br>"));
?>
Example #16: Forcing text in a string to be all uppercase or lowercase
Please vote on what you think of this diary lesson :)
[back]
12/29/99 "Building an opt-in email list"
[next]
PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com
Copyright 2000 php-scripts.com Last Modified 01/6/00 05:50