Mastering Regular Expressions in PHP

Written by Dennis Pallett

Continued from page 1

That's about it for regular expressions. It seems very difficult, but once you grasp it is extremely easy yet one ofrepparttar most powerful tools when programming in PHP. I can't countrepparttar 105072 number of times regex has saved me from hours of coding difficult text functions.

An Example What would a good tutorial be without some real examples? Let's first have a look at a simple e-mail validation function. An e-mail address must start with letters or numbers, then have a @, then a domain, ending with an extension. The regex for that would be something like this: ^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$

Let me quickly explain that regex. Basically,repparttar 105073 first part says that it must all be letters or numbers. Then we getrepparttar 105074 @, and after that there should be letters and/or numbers again (the domain). Finally we check for a period, and then for an extension. The code to use this regex looks like this:


// Good e-mail $good = "";

// Bad e-mail $bad = "blabla@blabla";

// Let's checkrepparttar 105075 good e-mail if (preg_match("/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/", $good)) { echo "Valid e-mail"; } else { echo "Invalid e-mail"; }

echo '<br />';

// And checkrepparttar 105076 bad e-mail if (preg_match("/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/", $bad)) { echo "Valid e-mail"; } else { echo "Invalid e-mail"; }


The result of this would be "Valid E-mail. Invalid E-mail", of course. We have just checked if an e-mail address is valid. If you wraprepparttar 105077 above code in a function, you've got yourself a e-mail validation function. Keep in mind though thatrepparttar 105078 regex isn't perfect: after all, it doesn't check whetherrepparttar 105079 extension is too long, does it? Because I want to keep this tutorial short, I won't giverepparttar 105080 full fledged regex, but you can find it easily via Google.

Another Example Another great example would be a telephone number. Say you want to verify telephone numbers and make sure they were inrepparttar 105081 correct format. Let's assume you wantrepparttar 105082 numbers to be inrepparttar 105083 format of xxx-xxxxxxx. The code would look something like this:


// Good number $good = "123-4567890";

// Bad number $bad = "45-3423423";

// Let's checkrepparttar 105084 good number if (preg_match("/d{3}-d{7}/", $good)) { echo "Valid number"; } else { echo "Invalid number"; }

echo '<br />';

// And checkrepparttar 105085 bad number if (preg_match("/d{3}-d{7}/", $bad)) { echo "Valid number"; } else { echo "Invalid number"; }


The regex is fairly simple, because we use d. This basically means "match any digit" withrepparttar 105086 length behind it. In this example it first looks for 3 digits, then a '-' (hyphen) and finally 7 digits. Works perfectly, and does exactly what we want.

What exactly is possible with Regular Expressions? Regular expressions are actually one ofrepparttar 105087 most powerful tools in PHP, or any other language for that matter (you can use it in your mod_rewrite rules as well!). There is so much you can do with regex, and we've only scratchedrepparttar 105088 surface in this tutorial with some very basic examples.

If you really want to dig into regex I suggest you search on Google for more tutorials, and try to learnrepparttar 105089 regex syntax. It isn't easy, and there's quite a steep learning curve (in my opinion), butrepparttar 105090 best way to learn is to go through a lot of examples, and try to translate them in plain English. It really helps you learnrepparttar 105091 syntax.

Inrepparttar 105092 future I will dedicate a complete article to strictly examples, including more advanced ones, without any explanation. But for now, I can only give you links to other tutorials: The 30 Minute Regex Tutorial

Dennis Pallett is a young tech writer, with much experience in ASP, PHP and other web technologies. He enjoys writing, and has written several articles and tutorials. To find more of his work, look at his websites at, and


Written by

Continued from page 1

    <Back to Page 1 © 2005
Terms of Use