Make your own free website on Tripod.com

Visit the Official PHP Website

Search this Site

PHP Programming

By James N Hitz

REGULAR EXPRESSIONS

A regular expression is a hypocrite. It is says one thing but means and does another thing all together. Matter of factly, it is a pattern of characters used for searching and processing text.

Think of it this way. Supposing you had a feed-back form on your website where users enter their names, email addresses and a message/comment. , you will want to test the data entered to see that the data has been entered correctly. Just think of how you could test an email address for example to ensure it is in the following format:

xxxxxxx@yyyyyy.zzz

Of course xxxxxxxx is the user name (must not be blank), yyyyyy.zzz is the server (must not be blank). Of course zzz will be two or three characters for domains like .ke or .com (although now they have introduced funny domains... they just thrive in making our programming job tougher).

You may opt to use PHP's equality operators. These as you may recall include:

  1. equal to (==)
  2. not equal to (!=)
  3. less than (<)
  4. less than or equal to (<=)
  5. greater than (>)
  6. greater than or equal to (>=)

Examples of how these operators may be used include:

to test if the age is less than twenty

if($age <20){
	do something;
}

to test if items ordered are 12 (a dozen)

if($itemsOrdered == 12){
	do something;
}

to test if the action is NOT equal to "login"

if($action != "login"){
	do something;
}

to test the status

if($status == "valid"){
	do something;
}

Relational operators work fine. But put ALL your hope and trust in diem' operators and you're doomed.

Just as a reminder, notice that we are using double equals (==) to test for equality and not (=) which stands for assignement.

With alot of creativity and many lines of coding, you MAY eventually be able to test the email address to ensure it adheres to the above format. But there is no lying about it, it's gonna be TOUGH and you will have to use many string manipulation functions to get any usable form of validation!

Regular expressions to the rescue... ... ... Away!

<< Introduction to this Lesson | Very Little Coding >>

JamHitz Productions