Chapter 11. Advanced Goodness

So far, this book has made an attempt to draw out the best of the PHP language. We have covered most of the basic concepts of PHP and discussed the cream of each topic. In this chapter, we’ll look at some of the more advanced features of PHP and end with a summary of helpful references and resource materials so that you can go beyond this book into much deeper PHP waters if and when you so choose.

Regular Expressions

The first advanced area we will look at is that of regular expressions. Regular expressions provide a more advanced way to match patterns within a given string. Although there are many string functions available within PHP, there are still some tasks that you may want to perform that only a regular expression can accomplish. There are two types of regular expressions: Portable Operating System Interface for UniX (POSIX) and Perl-compatible. Because the Perl-compatible expressions are a little faster and more robust, we will only look at them here. There are three general uses for regular expressions: string matching, string substituting, and string splitting.

String Matching

Let’s look at string matching first. When you are looking for a certain string or pattern within a provided string, you have to delimit the pattern. You generally do this with the forward slash character (/), but you can use any other nonalphanumeric character (other than the backslash) to do the same thing. So, if you are looking for a string pattern of “fox,” ...

Get PHP: The Good Parts now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.