T-XU.COM      
Home |
  Home>Computer Programming>CGI>
Mastering Regular Expressions in PHP
By Dennis Pallett        [Hits: 9293]



What areRegular Expressions? A regular expression is a patternthat can match various text strings. Using regular expressionsyou can find (and replace) certain text patterns, for example"all the words that begin with the letter A" or "find onlytelephone numbers". Regular expressions are often used invalidation classes, because they are a really powerful tool toverify e-mail addresses, telephone numbers, street addresses,zip codes, and more.

In this tutorial I will show you how regular expressions work inPHP, and give you a short introduction on writing your ownregular expressions. I will also give you several exampleregular expressions that are often used. Regular Expressions inPHP Using regex (regular expressions) is really easy in PHP, andthere are several functions that exist to do regex finding andreplacing. Let's start with a simple regex find.

Have a look at the documentation of the preg_match function. As youcan see from the documentation, preg_match is used to perform aregular expression. In this case no replacing is done, only asimple find. Copy the code below to give it a try.
<?php

// Example string $str = "Let's find the stuff<bla>in between</bla> these two previousbrackets";

// Let's perform the regex $do =preg_match("/<bla>(.*)</bla>/", $str,$matches);

// Check if regex was successful if ($do = true) { // Matchedsomething, show the matched string echohtmlentities($matches['0']);

// Also how the text in between the tags echo '<br />' .$matches['1']; } else { // No Match echo "Couldn't find amatch"; }

?>
After having run the code, it's probably a good ideaif I do a quick run through the code. Basically, the whole coreof the above code is the line that contains the preg_match. Thefirst argument is your regex pattern. This is probably the mostimportant. Later on in this tutorial, I will explain some basicregular expressions, but if you really want to learn regularexpression then it's best if you look on Google for specificregular expression examples.

The second argument is the subject string. I assume that needsno explaining. Finally, the third argument can be optional, butif you want to get the matched text, or the text in betweensomething, it's a good idea to use it (just like I used it inthe example). The preg_match function stops after it has foundthe first match. If you want to find ALL matches in a string,you need to use the preg_match_allfunction. That works pretty much the same, so there is noneed to separately explain it.

Now that we've had finding, let's do a find-and-replace, withthe preg_replacefunction. The preg_replace function works pretty similar tothe preg_match function, but instead there is another argumentfor the replacement string. Copy the code below, and run it.
<?php

// Example string $str = "Let's replace the<bla>stuff between</bla> the bla brackets";

// Do the preg replace $result = preg_replace("/<bla>(.*)</bla>/", "<bla>newstuff</bla>", $str);

echo htmlentities($result); ?>
The result would then bethe same string, except it would now say 'new stuff' between thebla tags. This is of course just a simple example, and moreadvanced replacements can be done.

You can also use keys in the replacement string. Say you stillwant the text between the brackets, and just add something? Youuse the $1, $2, etc keys for those. For example:
<?php

// Example string $str = "Let's replace the<bla>stuff between</bla> the bla brackets";

// Do the preg replace $result = preg_replace("/<bla>(.*)</bla>/", "<bla>newstuff (the old: $1)</bla>", $str);

echo htmlentities($result); ?>
This would then print"Let's replace the new stuff (the old: stuff between)the bla brackets". $2 is for the second "catch-all", $3 for thethird, etc.

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

AnExample What would a good tutorial be without some realexamples? Let's first have a look at a simple e-mail validationfunction. An e-mail address must start with letters or numbers,then have a @, then a domain, ending with an extension. Theregex 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, the first partsays that it must all be letters or numbers. Then we get the @,and after that there should be letters and/or numbers again (thedomain). Finally we check for a period, and then for anextension. The code to use this regex looks like this:
<?php

// Good e-mail $good = "john@example.com";

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

// Let's check the 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 check the 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. InvalidE-mail", of course. We have just checked if an e-mail address isvalid. If you wrap the above code in a function, you've gotyourself a e-mail validation function. Keep in mind though thatthe regex isn't perfect: after all, it doesn't check whether theextension is too long, does it? Because I want to keep thistutorial short, I won't give the full fledged regex, but you canfind it easily via Google.

AnotherExample Another great example would be a telephonenumber. Say you want to verify telephone numbers and make surethey were in the correct format. Let's assume you want thenumbers to be in the format of xxx-xxxxxxx. The code would looksomething like this:
<?php

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

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

// Let's check the good number if(preg_match("/d{3}-d{7}/", $good)) { echo "Validnumber"; } else { echo "Invalid number"; }

echo '<br />';

// And check the bad number if(preg_match("/d{3}-d{7}/", $bad)) { echo "Validnumber"; } else { echo "Invalid number"; }

?>
The regex is fairly simple, because we use d. Thisbasically means "match any digit" with the length behind it. Inthis example it first looks for 3 digits, then a '-' (hyphen)and finally 7 digits. Works perfectly, and does exactly what wewant.

What exactly ispossible with Regular Expressions? Regular expressionsare actually one of the most powerful tools in PHP, or any otherlanguage for that matter (you can use it in your mod_rewriterules as well!). There is so much you can do with regex, andwe've only scratched the surface in this tutorial with some verybasic examples.

If you really want to dig into regex I suggest you search onGoogle for more tutorials, and try to learn the regex syntax. Itisn't easy, and there's quite a steep learning curve (in myopinion), but the best way to learn is to go through a lot ofexamples, and try to translate them in plain English. It reallyhelps you learn the syntax.

In the future I will dedicate a complete article to strictlyexamples, including more advanced ones, without any explanation.But for now, I can only give you links to other tutorials: The30 Minute Regex Tutorial Regular-Expressions.info
  Top Articles
*CGI Security Issues
*Clever Profit Growth Software
*Why Aren't You Using CGI
*How to Stop Digital Thieves wi
*Open Source Scripts
*5 CGI Scripts You Must Use to
*Quick Intro to PHP Development
*PHP and Cookies; a good mix!
*PHP On-The-Fly!
*this is a test
*Screen scraping your way into
*ASP, CGI and PHP Scripts and R
  Related Articles
*ASP, CGI and PHP Scripts and R
*Screen scraping your way into
*PHP and Cookies; a good mix!
*Open Source Scripts
*PHP On-The-Fly!
*this is a test
*An Extensive Examination of th
*Track your visitors, using PHP
*Autoresponders With PHP
*PHP:Form Series, Part 1: Valid
*Password Protection and File I
*Better Writing: What Works and


Prev: Practice   Next: Sell Yourself First...Your Key To More Sales



Home | Site Map | Bookmark this site | T-XU RSS
Copyright 2007 T-XU.com - All Rights Reserved Worldwide.