- for a FREE 30-Day Trial

eBusiness Help
Prevent PC freezes and crashes
->> Free system check
Rackspace managed Hosting: Fanatical Support™ and Guaranteed 100% network uptime.
1 hour hardware fix and many more business critical support features.
Sweet deals for iEntry members! Out with the old, in with the new...
if you have equipment that qualifies for trade-in, get a discount of up to $720* off the Web price on featured IBM ® ThinkPad ® notebooks.

WebProWorld Dev Forum

Live chat for my site
Wondering how can I put a live chat for my site !!! I've just put a forum yesterday and wanted to put a live chat there. Any idea - welcome.
Click to read more...

Reply From PHP Form
I have this code below on a contact form on a website. The form uses a POST method and PHP_SELF on submit. The code works fine, but on receiving the form results, I can't reply to the email address.
Click to read more...

Help! Images not loading
Kathleen's "client" (or portfolio) page on her website is not loading right for me. About half the images don't load and "red x" for me eventually. However on her identical laptop, they load fine.
Click to read more...



Recent Articles

Meet the Community at OSBC
Panel with Brian Behlendorf from Apache/Collabnet, Josh from PostgreSQL, Chris Hoffman from Mozilla, Larry Wall from Perl and David Wheeler from Bricolage. Moderated by Christian Einfeldt, "How these communities result..."

Serialize This - Saving Objects in PHP
When building my website "Crossword Heaven" I came across a problem. I created a PHP object called "crossword" but needed to save the information in the object to a database. Now considering that this object contained a lot of information this was not an easy thing to do. Or was it?

What if Intelliseek Bought Technorati?
The more I think about Intelliseek, the more I think they'll either try to muscle Technorati out of their market or they'll buy 'em. Intelliseek's BlogPulse Conversation Tracker is the sort of tool that I expected Technorati to build last year.

Google to Host Vlogs?
InternetWeek reports that Larry Page said today that the company will soon let the general public upload self-produced videos to Google's servers, partly in an effort to learn more about how to more efficiently search and display information about video-based data.

Mastering Regular Expressions in PHP
A regular expression is a pattern that can match various text strings. Using regular expressions you can find (and replace) certain text patterns, for example "all the words that begin with the letter A" or "find only telephone numbers".


04.15.05


Mastering Regular Expressions In PHP

By Dennis Pallett

What are Regular Expressions?

A regular expression is a pattern that can match various text strings. Using regular expressions you can find (and replace) certain text patterns, for example "all the words that begin with the letter A" or "find only telephone numbers". Regular expressions are often used in validation classes, because they are a really powerful tool to verify e-mail addresses, telephone numbers, street addresses, zip codes, and more.

In this tutorial I will show you how regular expressions work in PHP, and give you a short introduction on writing your own regular expressions. I will also give you several example regular expressions that are often used.

Regular Expressions in PHP

Using regex (regular expressions) is really easy in PHP, and there are several functions that exist to do regex finding and replacing. Let's start with a simple regex find.

Have a look at the documentation of the preg_match function (http://php.net/preg_match). As you can see from the documentation, preg_match is used to perform a regular expression. In this case no replacing is done, only a simple 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 previous brackets";

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

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

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

?>


Free Software Archive:
Enterprise and Home Networking Downloads

After having run the code, it's probably a good idea if I do a quick run through the code. Basically, the whole core of the above code is the line that contains the preg_match. The first argument is your regex pattern. This is probably the most important. Later on in this tutorial, I will explain some basic regular expressions, but if you really want to learn regular expression then it's best if you look on Google for specific regular expression examples.

The second argument is the subject string. I assume that needs no explaining. Finally, the third argument can be optional, but if you want to get the matched text, or the text in between something, it's a good idea to use it (just like I used it in the example).

The preg_match function stops after it has found the first match. If you want to find ALL matches in a string, you need to use the preg_match_all function (http://www.php.net/preg_match_all). That works pretty much the same, so there is no need to separately explain it.

Now that we've had finding, let's do a find-and-replace, with the preg_replace function (http://www.php.net/preg_replace). The preg_replace function works pretty similar to the preg_match function, but instead there is another argument for 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>new stuff</bla>", $str);

echo htmlentities($result);
?>



The result would then be the same string, except it would now say 'new stuff' between the bla tags. This is of course just a simple example, and more advanced replacements can be done.

You can also use keys in the replacement string. Say you still want the text between the brackets, and just add something? You use 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>new stuff (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 the third, etc.

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

Read the Rest of the Article.

About the Author:
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 http://www.phpit.net, http://www.aspit.net and http://www.ezfaqs.com.

About DevNewz
DevNewz has assembled experts around the world to deliver helpful advice to application developers. Our in-house news staff focuses on keeping you updated with the latest new software and trends in application development. DevNewz provides Knowlege For Application Developers.

DevNewz is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
DatabaseProNews.com SQLProNews.com
ITcertificationNews.com SysAdminNews.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITmanagementNews.com


-- DevNewz is an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2005 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article

Knowlege For Application Developers DevNewz News Archives About Us Feedback DevNewz Home Page About Article Archive News Downloads WebProWorld Forums Jayde iEntry Advertise Contact