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.

Table 4-8. POSIX anchors<br />

Anchor Matches<br />

^ Start of string<br />

$ End of string<br />

[[::]] End of word<br />

A word boundary is defined as the point between a whitespace character and an<br />

identifier (alphanumeric or underscore) character:<br />

ereg('[[::]]', 'the Burgundy exploded'); // returns false<br />

ereg('gun', 'the Burgundy exploded'); // returns true<br />

Note that the beginning and end of a string also qualify as word boundaries.<br />

Functions<br />

There are three categories of functions for POSIX-style regular expressions: matching,<br />

replacing, and splitting.<br />

Matching<br />

The ereg( ) function takes a pattern, a string, and an optional array. It populates the<br />

array, if given, and returns true or false depending on whether a match for the pattern<br />

was found in the string:<br />

$found = ereg(pattern, string [, captured ]);<br />

For example:<br />

ereg('y.*e$', 'Sylvie'); // returns true<br />

ereg('y(.*)e$', 'Sylvie', $a); // returns true, $a is array('Sylvie', 'lvi')<br />

The zeroth element of the array is set to the entire string being matched against. The<br />

first element is the substring that matched the first subpattern, the second element is<br />

the substring that matched the second subpattern, and so on.<br />

The eregi( ) function is a case-insensitive form of ereg( ). Its arguments and return<br />

values are the same as those for ereg( ).<br />

Example 4-1 uses pattern matching to determine whether a credit-card number<br />

passes the Luhn checksum and whether the digits are appropriate for a card of a specific<br />

type.<br />

Example 4-1. Credit-card validator<br />

// The Luhn checksum determines whether a credit-card number is syntactically<br />

// correct; it cannot, however, tell if a card with the number has been issued,<br />

// is currently active, or has enough space left to accept a charge.<br />

POSIX-Style Regular Expressions | 101<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!