05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

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

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

The Basics<br />

Most characters in a regular expression are literal characters, meaning that they<br />

match only themselves. For instance, if you search for the regular expression "cow" in<br />

the string "Dave was a cowhand", you get a match because "cow" occurs in that string.<br />

Some characters, though, have special meanings in regular expressions. For instance,<br />

a caret (^) at the beginning of a regular expression indicates that it must match the<br />

beginning of the string (or, more precisely, anchors the regular expression to the<br />

beginning of the string):<br />

ereg('^cow', 'Dave was a cowhand'); // returns false<br />

ereg('^cow', 'cowabunga!'); // returns true<br />

Similarly, a dollar sign ($) at the end of a regular expression means that it must<br />

match the end of the string (i.e., anchors the regular expression to the end of the<br />

string):<br />

ereg('cow$', 'Dave was a cowhand'); // returns false<br />

ereg('cow$', "Don't have a cow"); // returns true<br />

A period (.) in a regular expression matches any single character:<br />

ereg('c.t', 'cat'); // returns true<br />

ereg('c.t', 'cut'); // returns true<br />

ereg('c.t', 'c t'); // returns true<br />

ereg('c.t', 'bat'); // returns false<br />

ereg('c.t', 'ct'); // returns false<br />

If you want to match one of these special characters (called a metacharacter), you<br />

have to escape it with a backslash:<br />

ereg('\$5\.00', 'Your bill is $5.00 exactly'); // returns true<br />

ereg('$5.00', 'Your bill is $5.00 exactly'); // returns false<br />

Regular expressions are case-sensitive by default, so the regular expression "cow"<br />

doesn’t match the string "COW". If you want to perform a case-insensitive POSIX-style<br />

match, you can use the eregi( ) function. With Perl-style regular expressions, you<br />

still use preg_match( ), but specify a flag to indicate a case-insensitive match (as you’ll<br />

see when we discuss Perl-style regular expressions in detail later in this chapter).<br />

So far, we haven’t done anything we couldn’t have done with the string functions<br />

we’ve already seen, like strstr( ). The real power of regular expressions comes<br />

from their ability to specify abstract patterns that can match many different character<br />

sequences. You can specify three basic types of abstract patterns in a regular<br />

expression:<br />

• A set of acceptable characters that can appear in the string (e.g., alphabetic characters,<br />

numeric characters, specific punctuation characters)<br />

• A set of alternatives for the string (e.g., "com", "edu", "net", or "org")<br />

• A repeating sequence in the string (e.g., at least one but no more than five<br />

numeric characters)<br />

96 | Chapter 4: Strings<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

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

Saved successfully!

Ooh no, something went wrong!