Skip to Content
Beginning PHP 5.3
book

Beginning PHP 5.3

by Matt Doyle
October 2009
Beginner
836 pages
19h 37m
English
Wrox
Content preview from Beginning PHP 5.3

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: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP and MySQL™ Phrasebook

PHP and MySQL™ Phrasebook

Christian Wenz
PHP 7 Programming Blueprints

PHP 7 Programming Blueprints

Jose Palala, Martin Helmich
PHP 5 Unleashed

PHP 5 Unleashed

John Coggeshall
Pro PHP Programming

Pro PHP Programming

Peter MacIntyre, Brian Danchilla, Mladen Gogala

Publisher Resources

ISBN: 9780470413968Purchase book