Menu Samaritan's Purse

PREG_SPLIT

Split string by a regular expression

SYNTAX

preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] ) : array

PARAMETERS

pattern

The pattern to search for, as a string.

subject

The input string.

limit

If specified, then only substrings up to limit are returned with the rest of the string being placed in the last substring. A limit of -1 or 0 means "no limit".

flags

flags can be any combination of the following flags (combined with the | bitwise operator):

PREG_SPLIT_NO_EMPTY Only non-empty pieces will be returned.
PREG_SPLIT_DELIM_CAPTURE Parenthesized expression in the delimiter pattern will be captured and returned as well.
PREG_SPLIT_OFFSET_CAPTURE For every occurring match the appendant string offset will also be returned. Note that this changes the return value in 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.

RETURN

Returns an array containing substrings of subject split along boundaries matched by pattern, or FALSE on failure.

EXAMPLES

PATTERN SUBJECT

Array
(
    [0] => coding
    [1] => programming
    [2] => scripting
    [3] =>
)

LIMIT

Array
(
    [0] => coding
    [1] => programming, scripting,
)

FLAGS | PREG_SPLIT_NO_EMPTY

Array
(
    [0] => coding
    [1] => programming
    [2] => scripting
)

FLAGS | PREG_SPLIT_DELIM_CAPTURE

Array
(
    [0] => coding
    [1] => ,
    [2] => programming
    [3] => ,
    [4] => scripting
    [5] => ,
    [6] =>
)

FLAGS | PREG_SPLIT_OFFSET_CAPTURE

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

    [1] => Array
        (
            [0] => programming
            [1] => 8
        )

    [2] => Array
        (
            [0] => scripting
            [1] => 21
        )

    [3] => Array
        (
            [0] =>
            [1] => 32
        )

)

PHP