Menu Samaritan's Purse

PREG_MATCH

Perform a regular expression match

SYNTAX

preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] ) : int

PARAMETERS

pattern

The pattern to search for, as a string.

subject

The input string.

matches

If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

flags

flags can be a combination of the following flags:

PREG_OFFSET_CAPTURE For every occurring match the appendant string offset (in bytes) will also be returned. Note that this changes the value of matches into an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
PREG_UNMATCHED_AS_NULL Unmatched subpatterns are reported as NULL; otherwise they are reported as an empty string.
offset

Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search (in bytes).

Note: Using offset is not equivalent to passing substr($subject, $offset) to preg_match() in place of the subject string, because pattern can contain assertions such as ^, $ or (?<=x). Alternatively, to avoid using substr(), use the \G assertion rather than the ^ anchor, or the A modifier instead, both of which work with the offset parameter.

RETURN

preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

Warning: This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the identity operator (===) for testing the return value of this function.

EXAMPLES

PATTERN SUBJECT | CASE-SENSITIVE

0

PATTERN SUBJECT | CASE-INSENSITIVE

1

MATCHES

Array
(
    [0] => PHP
)

FLAGS | PREG_OFFSET_CAPTURE

Array
(
    [0] => Array
        (
            [0] => pregoffsetcapture
            [1] => 0
        )

    [1] => Array
        (
            [0] => preg
            [1] => 0
        )

    [2] => Array
        (
            [0] => offset
            [1] => 4
        )

    [3] => Array
        (
            [0] => capture
            [1] => 10
        )

)

FLAGS | PREG_UNMATCHED_AS_NULL

array (
0 => 'pregas',
1 => 'preg',
2 => '',
3 => 'as',
)
array (
0 => 'pregas',
1 => 'preg',
2 => NULL,
3 => 'as',
)

OFFSET

Array
(
    [0] => Array
        (
            [0] => offset
            [1] => 7
        )

)

WORD BOUNDARY

found
not found

NAMED SUBPATTERN

Array
(
    [0] => year: 2001
    [name] => year
    [1] => year
    [digit] => 2001
    [2] => 2001
)

URL

second-level and top-level domain: php.net

PHP