Chapter 18. String Matching with Regular Expressions
In Chapter 5 you explored many of PHP's powerful string manipulation functions, such as strstr()
for finding text within a string and str_replace()
for searching and replacing text within a string.
In this chapter you learn about PHP's even more powerful regular expression functions. These give you a lot more flexibility than the regular string-matching functions because they let you compare a string against a pattern of characters rather than against a fixed string. For example, you can search a string for a pattern of characters comprising exactly three digits, followed by either a comma or a dot, followed by from one to four letters.
Within a regular expression you use special symbols such as ?
and ^
to specify a pattern. Because patterns can get quite complex, it can be hard to decipher regular expressions when you first start out. However, with a bit of practice, as well as some of the hints in this chapter, you'll find that you can read regular expressions much more quickly and easily.
Although regular expressions aren't essential to PHP programming — you can usually do a similar job with the string matching functions and a loop or two — they're your best bet when you need to match patterns. For one thing, a single regular expression is usually much more compact and intuitive than a whole series of string matches and loops. The expression will often run much quicker, too.
By the time you've read this chapter you'll know about: ...
Get Beginning PHP 5.3 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.