FREE Download of Paessler Site Inspector 4

eBiz Help
Serve ads that make sense. And money.
->> More Info
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 20%* off the Web price on featured IBM ® ThinkPad ® notebooks.

WebProWorld Dev Forum

CSS and the border-color command
I need some help, no matter where I search I can't find "how to" make a border color, where the outside borders AND the inside borders are colored. This calendar has bordercolor="" for now, however that doesn't validate. I need every border colored. Can anyone help?
Click to read more...

Ask for help with php program
Next php code is being included in a table on a link page. For each category there is a script like this one. But it gives an error for the last php tag. can anybody tell me what's wrong?
Click to read more...

CSS or PHP for SEO
I am looking into redesigning my site. I’d like to excuse my programming inexperience at this point. In terms of SEO what are the differences between using CSS or PHP?
Click to read more...



Recent Articles

Web Development Processes and Technical Environments
The process of Web application development is critical to the success of web-based projects.

Why Attention.xml Could Change PR Forever
Attention.xml is a new technology standard that's being proselytized by influencers like ...

SVG and Flash as the Same Species
We can name several technologies for displaying 2D interactive vector graphics on the web however, two names rises among other contenders.

A JavaScript That Kills Google AutoLink Links
Chris and Gurtie at Search Guild have published a working script that webmasters can insert into their webpages that kills Google AutoLink links.

Get PHP pages indexed in the Search engines
A simple method to convert files from Php to Html Extensions, on an Apache Server

Web Hosting: 4 Factors in Choosing a Hosting Company
When choosing a web hosting company there are a whole lot of different factors that should go into your decision making process.

PHP and Cookies; a Good Mix!
Cookies have long been used in PHP scripts, and are a very useful function. But what exactly are cookies? Maybe you have used then, but you still don't know exactly what they are. Or you are completely new to cookies?

PHP to HTML – Getting Dynamic Pages Indexed
Most will agree Php has become the common language for creating dynamic Websites. Although the language is simple and efficient, developers are finding trouble getting traffic to their Php site.

Do-It-Yourself Usability Testing: Your Questions Answered
So, if you have followed all of our Usability articles, outlining how to create useful content, layout, and navigation, then you may feel your website is set and ready and usable.

04.06.05


Mastering Regular Expressions In PHP

By Maricon Williams

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 Download of Paessler Site Inspector 4

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 TheDevWeb
TheDevWeb examines the best tools and techniques to help you keep up with all the latest trends in the fast paced world of web development. Excellence in Development Starts Here

TheDevWeb is brought to you by:

SecurityConfig.comNetworkingFiles.com
NetworkNewz.comWebProASP.com
DatabaseProNews.comSQLProNews.com
ITcertificationNews.comSysAdminNews.com
LinuxProNews.comWirelessProNews.com
CProgrammingTrends.comITmanagementNews.com



-- TheDevWeb 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

Excellence in Development Starts HereTheDevWebNewsArchivesAbout UsFeedbackTheDevWeb Home PageAboutArticle ArchiveNewsDownloadsWebProWorld ForumsJaydeiEntryAdvertiseContact