25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

90 ” Strings And Patterns<br />

lier in this chapter. Therefore, you should use them only when appropriate—that is,<br />

when using the simpler functions is either impossible or so complicated that it’s not<br />

worth the effort.<br />

A regular expression is a string that describes a set of matching rules. The simplest<br />

possible regular expression is one that matches only one string; for example, Davey<br />

matches only the string “Davey”. In fact, such a simple regular expression would be<br />

pointless, as you could just as easily perform the match using strpos(), which is a<br />

much faster alternative.<br />

The real power of regular expressions comes into play when you don’t know the<br />

exact string that you want to match. In this case, you can specify one or more metacharacters<br />

and quantifiers, which do not have a literal meaning, but instead stand to<br />

be interpreted in a special way.<br />

In this chapter, we will discuss the basics of regular expressions that are required<br />

by the exam. More thorough coverage is provided by the <strong>PHP</strong> manual, or by one of<br />

the many regular expression books available (most notably, Mastering Regular Expressions,<br />

by Jeffrey Friedl, published by O’Reilly Media).<br />

Delimiters<br />

A regular expression is always delimited by a starting and ending character. Any character<br />

can be used for this purpose (as long as the beginning and ending delimiter<br />

match); since any occurrence of this character inside the expression itself must be<br />

escaped, it’s usually a good idea to pick a delimiter that isn’t likely to appear inside<br />

the expression. By convention, the forward slash is used for this purpose—although,<br />

for example, another character like the octothorpe is sometimes used when dealing<br />

with pathnames or URLs.<br />

Metacharacters<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

The term “metacharacter” is a bit of a misnomer—as a metacharacter can actually<br />

be composed of more than one character. However, every metacharacter represents<br />

a single character in the matched expression. Here are the most common ones:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!