1 / 5

PHP – Pattern matching, search and replacement

PHP – Pattern matching, search and replacement. The PCRE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl 5 There are a few differences preg_grep, preg_match, preg_replace. PHP – search and replacement. preg_replace

emmett
Download Presentation

PHP – Pattern matching, search and replacement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP – Pattern matching, search and replacement • The PCRE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl 5 • There are a few differences • preg_grep, preg_match, preg_replace

  2. PHP – search and replacement • preg_replace • Perform a regular expression search and replace • preg_replace( pattern, replacement, subject [, int limit] ) • Searches subject for matches to pattern (a regular expression) and replaces them with replacement

  3. PHP – search and replacement • preg_replace( pattern, replacement, subject [, int limit] ) • Limit is optional • If not specified or –1  all matches are replaced (global) • If specified  the first limit matches will be replaced

  4. PHP – search and replacement • $colors = "red blue yellow red red green"; • $result = preg_replace( "/red/", "purple", $colors, 2 ); • echo “$result"; •  purple blue yellow purple red green

  5. PHP – regular expression • There are modifiers: i, e, s, m, .. • There are meta-characters: ?, *, +, \w, \d, .. • Remember the pattern matched: use ( )  back reference with $1, $2, .. •  explore at www.php.net (search for preg_replace)

More Related