Prev | Current Page 310 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"


The optional input parameter flags was added in PHP version 4.3. It accepts one
value, PREG_GREP_INVERT. Passing this flag will result in retrieval of those array elements
that do not match the pattern.
Searching for a Pattern
The preg_match() function searches a string for a specific pattern, returning TRUE if it
exists, and FALSE otherwise. Its prototype follows:
int preg_match(string pattern, string string [, array matches]
[, int flags [, int offset]]])
The optional input parameter pattern_array can contain various sections of the
subpatterns contained in the search pattern, if applicable. Here??™s an example that
uses preg_match() to perform a case-insensitive search:
$line = "vim is the greatest word processor ever created!";
if (preg_match("/\bVim\b/i", $line, $match)) print "Match found!";
?>
For instance, this script will confirm a match if the word Vim or vim is located, but
not simplevim, vims, or evim.
Matching All Occurrences of a Pattern
The preg_match_all() function matches all occurrences of a pattern in a string,
assigning each occurrence to an array in the order you specify via an optional input
parameter. Its prototype follows:
int preg_match_all(string pattern, string string, array pattern_array
[, int order])
244 CHAPTER 9 ?–  STRINGS AND REGULAR EXPRESSIONS
The order parameter accepts two values:
??? PREG_PATTERN_ORDER is the default if the optional order parameter is not
included.


Pages:
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322