LXXXVI. Regular Expression Functions (Perl-Compatible)

The syntax for patterns used in these functions closely resembles Perl. The expression should be enclosed in the delimiters, a forward slash (/), for example. Any character can be used for delimiter as long as it's not alphanumeric or backslash (\). If the delimiter character has to be used in the expression itself, it needs to be escaped by backslash. Since PHP 4.0.4, you can also use Perl-style (), {}, [], and <> matching delimiters.

The ending delimiter may be followed by various modifiers that affect the matching. See Pattern Modifiers.

PHP also supports regular expressions using a POSIX-extended syntax using the POSIX-extended regex functions..

Requirements

Regular expression support is provided by the PCRE library package, which is open source software, written by Philip Hazel, and copyright by the University of Cambridge, England. It is available at ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/.

Installation

Beginning with PHP 4.2.0 this function are enabled by default. For older versions you have to configure and compile PHP with --with-pcre-regex[=DIR] in order to use these functions. You can disable the pcre functions with --without-pcre-regex.

Runtime Configuration

This extension does not define any configuration directives.

Resource types

This extension does not define any resource types.

Predefined constants

PREG_PATTERN_ORDER PREG_SET_ORDER PREG_SPLIT_NO_EMPTY PREG_SPLIT_DELIM_CAPTURE

Examples

Example 1. Examples of valid patterns

  • /<\/\w+>/

  • |(\d{3})-\d+|Sm

  • /^(?i)php[34]/

  • {^\s+(\s+)?$}

Example 2. Examples of invalid patterns

  • /href='(.*)' - missing ending delimiter

  • /\w+\s*\w+/J - unknown modifier 'J'

  • 1-\d3-\d3-\d4| - missing starting delimiter

Table of Contents
Pattern Modifiers -- Describes possible modifiers in regex patterns
Pattern Syntax -- Describes PCRE regex syntax
preg_grep --  Return array entries that match the pattern
preg_match -- Perform a regular expression match
preg_match_all -- Perform a global regular expression match
preg_quote -- Quote regular expression characters
preg_replace -- Perform a regular expression search and replace
preg_replace_callback -- Perform a regular expression search and replace using a callback
preg_split -- Split string by a regular expression