« Why I Hate Baseball | Main | Yankee and Clemens Fan Interviews Clemens »

Regular Expressions

PHP is a simple programming language with a lot of horsepower. That's why I love working with it.

But to take advantage of some of its most useful features, you need to have at least some basic knowledge of regular expressions.

Regular Expressions have a syntax that is complicated, convoluted, and cantankerous. But once you master them, you become powerful indeed.

For instance, to parse a form field to confirm that it is a legitimate email address, this piece of pHP code will do the trick:

eregi("^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,3}$", $_POST['email']))

In a nutshell, the eregi function (case-insensitive) uses regular expressions defined here to look in a form field called email for:

  • alphanumeric characters, followed by
  • the "@" symbol, followed by
  • more alphanumeric characters, followed by
  • a period, followed by
  • two or three letters.

Powerful stuff indeed!

I'm no whiz of regex myself, but I'm making it a goal to get sharper on them over the next year. Here are some web resources that I've found especially helpful:

I Love Jack Daniel's Regex Cheat Sheet (pdf)
Visibone's JavaScript Regex Card
RegExLib's Cheat Sheet
ETextEditor's Video Tutorial and Cheat Sheet

TrackBack

TrackBack URL for this entry:
http://www.baldguyweb.com/mt/mt-tb.cgi/420

Post a comment


Get emailed notifications of new articles!
NOTE: I will not sell any email addresses I receive, and will not send any unsolicited emails, either. If you sign up for new article notifications, that is ALL you will receive, and you can cancel at any time. You have my word.
--Ron Enderland

Enter your email address:

About

This page contains a single entry from the blog posted on December 30, 2007 10:40 AM.

The previous post in this blog was Why I Hate Baseball.

The next post in this blog is Yankee and Clemens Fan Interviews Clemens.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 4.261