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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

PHP 5 Unleashed

PHP 5 Unleashed

John Coggeshall
PHP 7: Real World Application Development

PHP 7: Real World Application Development

Doug Bierer, Altaf Hussain, Branko Ajzele

Publisher Resources

ISBN: 9780470413968Purchase book