preg_split()
preg_split() — Split string by a regular expression.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
preg_split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $pattern | string | The pattern to search for, as a string. |
| $subject | string | The input string. |
| $limit | int | 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 | int | flags can be any combination of the following flags (combined with the | bitwise operator): PREG_SPLIT_NO_EMPTY If this flag is set, only non-empty pieces will be returned by preg_split(). PREG_SPLIT_DELIM_CAPTURE If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well. PREG_SPLIT_OFFSET_CAPTURE If this flag is set, for every occurring match the appendant string ... |
Return value
Returns an array containing substrings of subject split along boundaries matched by pattern, or false on failure.
Example
A runnable snippet:
// split the phrase by any number of commas or space characters, // which include " ", \r, \t, \n and \f $keywords = preg_split("/[\s,]+/", "hypertext language, programming"); print_r($keywords);
Output:
Array ( [0] => hypertext [1] => language [2] => programming )
See also
preg_quote(), explode(), preg_match(), preg_match_all(), preg_replace(), preg_last_error()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page preg_split() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.