Hudzilla.org - the homepage of Paul Hudson
Contents > Functions Wish List | Report Bug | About Me ]

4.8     Regular expressions

This is NOT the latest copy of this book; click here for the latest version.

So far we've been looking at string functions to help you manipulate strings, but what if you want more power - more control over your string manipulation?

Regular expressions, usually referred to as regexps, offer you that power, but are tricky to learn because they use complicated syntax to provide their power. The chances are that you have used regular expressions before, albeit perhaps on a fairly limited scale. For example, if you were at your shell prompt and typed "rm *.txt", you would expect all files ending with .txt to be removed.

Similarly, if you were to search your hard drive for all JPEG picture files, you would search for *.jpg - it would match foo.jpg, bar.jpg, fool.jpg, and barf.jpg. You've probably also used the ? regexp, which means "match precisely one character" - foo?.jpg would match only fool.jpg and not foobar.jpg.

Regular expressions offer a powerful and easy-to-use (if tricky to learn) way to perform powerful string matching and replacement functions. Regexps can:

  • Replace text

  • Test for a pattern within a string

  • Extract a substring from within a string

We'll be looking at all three of these uses in this section, as well as providing a comprehensive list of the different expressions you can use to work with all kinds of strings.

Before we start, there are two things you should be aware of: firstly, only use regular expressions when you have to. The set of string functions we just looked at are much faster, much easier to read, and much less hassle to use. Secondly, PHP contains two ways to perform regular expressions, known as POSIX-extended and Perl Compatible Regular Expressions (PCRE). The PCRE functions are more powerful than the POSIX ones, and they are faster too, so we will be using the PCRE functions here.





<< 4.7.18 Parsing a string into variables: parse_str()   4.8.1 Basic regexps with preg_match() and preg_match_all() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 05 Dec 2008

sdf

A PHP User - 05 Dec 2008

"globs nor regexps"

what does that mean?

A PHP User - 05 Dec 2008

what he describes above are globs nor regexps



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is eight plus four?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow