11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 9 • STRINGS AND REGULAR EXPRESSIONSwww.it-ebooks.infoMatching All Occurrences of a PatternThe preg_match_all() function matches all occurrences of a pattern in a string, assigning eachoccurrence 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 matches [, int flags] [, int offset]))The flags parameter accepts one of three values:• PREG_PATTERN_ORDER is the default if the optional flags parameter is not defined.PREG_PATTERN_ORDER specifies the order in the way that you might think mostlogical: $pattern_array[0] is an array of all complete pattern matches,$pattern_array[1] is an array of all strings matching the first parenthesizedregular expression, and so on.• PREG_SET_ORDER orders the array a bit differently than the default setting.$pattern_array[0] contains elements matched by the first parenthesized regularexpression, $pattern_array[1] contains elements matched by the secondparenthesized regular expression, and so on.• PREG_OFFSET_CAPTURE modifies the behavior of the returned matches parameter,changing how the array is populated by instead returning every matched stringand its corresponding offset as determined by the location of the match.Here’s how you would use preg_match_all() to find all strings enclosed in bold HTML tags:This returns the following:Zeev Suraski<strong>PHP</strong> GuruDelimiting Special Regular Expression CharactersThe function preg_quote() inserts a backslash delimiter before every character of special significance toregular expression syntax. These special characters include $ ^ * ( ) + = { } [ ] | \\ : < >. Its prototypefollows:string preg_quote(string str [, string delimiter])The optional parameter delimiter specifies what delimiter is used for the regular expression,causing it to also be escaped by a backslash. Consider an example:202

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

Saved successfully!

Ooh no, something went wrong!