Character classes are a defined set of characters. PHP holds some pre-defined classes for expressions :
Classes are created by putting a set of characters within the [square] brackets. Searching for a vowel may be done by using [aeiou] for example. The search will take each letter and do a search for it in the specified area.
Using a dash inside a class is a shortcut to specify "between". Using [a-f] will do a search for any lowercase letter between a to f.
Combinations can also be entered in classes. Using [a-zA-Z] will do a search for any lowercase or uppercase letter. This would also create the same result as using the [[:alpha:]] class in this example.
The ^ symbol has a different effect inside the square brackets. It will do a search that does not include the pattern specified. Using [^z] searches for any character other than lower case z.
Using this new information, you can revise the email address checker script like so...
Results :
Here is the breakdown of the search pattern...
We did not have to enter the capital letters for searching because the eregi is not case sensitive.
The first two samples are true as they contain all the correct elements.
The third sample is false as it is missing the period.
The fourth sample is false as it contains an ending more than 3 letters.
The fifth sample is false as it contains an ending of less than 2 letters.
This was a really complicated search pattern, but taking it one step at a time, you can see how it works. This also shows you can create a very simple or elaborate search pattern depending on your needs in a script.
Please note : The dash is used at the start of the patterns so it is not viewed as a range character. When the period is contained within the square brackets, it is viewed as an actual period and does not need to be escape backslashed. (Thank-you to Ryan Quigley for correcting our error previously seen on this page.)
| [[:alpha:]] | any letter |
| [[:digit:]] | any digit |
| [[:alnum:]] | any letter or digit |
| [[:space:]] | any white space |
| [[:upper:]] | any upper case letter |
| [[:lower:]] | any lower case letter |
| [[:punct:]] | any punctuation mark |
Classes are created by putting a set of characters within the [square] brackets. Searching for a vowel may be done by using [aeiou] for example. The search will take each letter and do a search for it in the specified area.
Using a dash inside a class is a shortcut to specify "between". Using [a-f] will do a search for any lowercase letter between a to f.
Combinations can also be entered in classes. Using [a-zA-Z] will do a search for any lowercase or uppercase letter. This would also create the same result as using the [[:alpha:]] class in this example.
The ^ symbol has a different effect inside the square brackets. It will do a search that does not include the pattern specified. Using [^z] searches for any character other than lower case z.
Using this new information, you can revise the email address checker script like so...
<?php
$value1 = "here@there.com";
$value2 = "here@there.ca";
$value3 = "here@therecom";
$value4 = "blah@noplace.yahoo";
$value5 = "cool@site.n";
$search_pattern = "^([0-9a-z]+)([-._0-9a-z]+)@([-._0-9a-z]+)(\.[a-z]{2,3}$)";
$result1 = eregi($search_pattern,$value1);
$result2 = eregi($search_pattern,$value2);
$result3 = eregi($search_pattern,$value3);
$result4 = eregi($search_pattern,$value4);
$result5 = eregi($search_pattern,$value5);
if ($result1) {echo "one is true. ";} else {echo "one is false. ";}
if ($result2) {echo "two is true. ";} else {echo "two is false. ";}
if ($result3) {echo "three is true. ";} else {echo "three is false. ";}
if ($result4) {echo "four is true. ";} else {echo "four is false. ";}
if ($result5) {echo "five is true. ";} else {echo "five is false. ";}
?>
$value1 = "here@there.com";
$value2 = "here@there.ca";
$value3 = "here@therecom";
$value4 = "blah@noplace.yahoo";
$value5 = "cool@site.n";
$search_pattern = "^([0-9a-z]+)([-._0-9a-z]+)@([-._0-9a-z]+)(\.[a-z]{2,3}$)";
$result1 = eregi($search_pattern,$value1);
$result2 = eregi($search_pattern,$value2);
$result3 = eregi($search_pattern,$value3);
$result4 = eregi($search_pattern,$value4);
$result5 = eregi($search_pattern,$value5);
if ($result1) {echo "one is true. ";} else {echo "one is false. ";}
if ($result2) {echo "two is true. ";} else {echo "two is false. ";}
if ($result3) {echo "three is true. ";} else {echo "three is false. ";}
if ($result4) {echo "four is true. ";} else {echo "four is false. ";}
if ($result5) {echo "five is true. ";} else {echo "five is false. ";}
?>
Results :
one is true. two is true. three is false. four is false. five is false.
Here is the breakdown of the search pattern...
| ^([0-9a-z]+) | The value should start with at least one number or letter. |
| ([-._0-9a-z]+) | The next characters should be at least one dash, period, underscore, number, or letter. |
| @ | The @ symbol required in any email address. |
| ([-._0-9a-z]+) | Another set of characters at least one dash, period, underscore, number, or letter. |
| (\.[a-z]{2,3}$) | The value should end with the period then either two or three letters. |
We did not have to enter the capital letters for searching because the eregi is not case sensitive.
The first two samples are true as they contain all the correct elements.
The third sample is false as it is missing the period.
The fourth sample is false as it contains an ending more than 3 letters.
The fifth sample is false as it contains an ending of less than 2 letters.
This was a really complicated search pattern, but taking it one step at a time, you can see how it works. This also shows you can create a very simple or elaborate search pattern depending on your needs in a script.
Please note : The dash is used at the start of the patterns so it is not viewed as a range character. When the period is contained within the square brackets, it is viewed as an actual period and does not need to be escape backslashed. (Thank-you to Ryan Quigley for correcting our error previously seen on this page.)

