About us Privacy Disclaimer Contact us
FAQ Help Advertising Feedback
Home Sitemap Search Donate us

  Home > Computer & I'net > Character Classes...

   Browse by title articles:
   What is hot:

Character Classes

Matching/Replacing

Functions - Intro

Functions - Arguments

Functions - Return Values

Include

Storage and re-use of images using PHP/GD - Part II
How to Tame Your Mouse
Interactive Scripts
HTML Basics
Prev articles123456789 10 1112Next articles



Character Classes


Computer & I'net articlesCharacter Classes

by David Stanley    



Character classes are a defined set of characters. PHP holds some pre-defined classes for expressions :

[[: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. ";}
?>

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 number, letter, period, dash or underscore.
@ The @ symbol required in any email address.
([0-9a-z.-_]+) Another set of characters at leaste one number, letter, period, dash or underscore.
(.[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.




-----------------
Article by David Stanley. Visit his site http://www.htmlite.com. Reprinted with permission.





Net Worth




  Disclaimer | Privacy | Terms of useCopyright © 2004 Nice2know.com