Skip to content

Core\Regex::split

Splits a text into pieces at every match of a pattern, and returns the pieces in an array. The matches themselves are not part of any piece. If there is no match, the array has one piece: the whole text.

Two matches next to each other give an empty piece. The keepEmpty: false option removes the empty pieces. The limit option sets the largest number of pieces, and the last piece then contains the rest of the text. A negative limit removes that many pieces from the end, and 0 returns the whole text as one piece.

Good to know: to split at one fixed text, such as a comma, Core\Str::split is simpler.

The examples show a split at commas, the limit and keepEmpty options, and reading the words of a search that a person typed.

Parameters

  • $subjectstringThe text to split.
  • $patternCore\Regex\Pattern|stringA Core\Regex\Pattern from Core\Regex::compile, or a pattern string with no options. A tainted string does not compile here.

Options

  • limitintoptionalThe largest number of pieces. The last piece contains the rest of the text. A negative number removes that many pieces from the end. 0 returns the whole text as one piece. The default is no limit.
  • keepEmptybooloptionalWhether empty pieces are kept. The default is true. With false, empty pieces are removed after limit is applied.

Return value

Returns array<string>. The pieces, from left to right. If there is no match, the array has one piece: the whole text.

Errors

  • RuntimeError$pattern is not a valid regular expression, or matching it against $subject needs more steps than the limit allows.

Examples

Split a list at commas

<?nvs
// Shows how to split a text into pieces at every match of a pattern.
string $colors = 'red, green,blue , yellow';
// `\s*,\s*` matches a comma and the spaces around it. The matches are removed.
// This prints ["red","green","blue","yellow"].
echo Core\Json::encode(Core\Regex::split($colors, '\s*,\s*')), "\n";
// There is no match here, so the array has one piece: the whole text.
// This prints ["red, green,blue , yellow"].
echo Core\Json::encode(Core\Regex::split($colors, ';')), "\n";
Output
["red","green","blue","yellow"]
["red, green,blue , yellow"]

Limit and empty pieces

<?nvs
// Shows how `limit` and `keepEmpty` change the pieces that `Core\Regex::split` returns.
string $row = 'a,,b,c';
// Two commas next to each other give an empty piece. This prints ["a","","b","c"].
echo Core\Json::encode(Core\Regex::split($row, ',')), "\n";
// `keepEmpty: false` removes the empty pieces. This prints ["a","b","c"].
echo Core\Json::encode(Core\Regex::split($row, ',', { keepEmpty: false })), "\n";
// `limit: 2` gives at most 2 pieces. The last piece contains the rest of the text.
// This prints ["a",",b,c"].
echo Core\Json::encode(Core\Regex::split($row, ',', { limit: 2 })), "\n";
// A negative limit removes pieces from the end. This prints ["a","","b"].
echo Core\Json::encode(Core\Regex::split($row, ',', { limit: -1 })), "\n";
Output
["a","","b","c"]
["a","b","c"]
["a",",b,c"]
["a","","b"]

Read the words of a search

<?nvs
// Shows how to read the words of a search that a person typed into a form.
string $search = ' summer dress, size 38 ';
// `[\s,]+` matches one or more spaces or commas. `keepEmpty: false` removes the
// empty pieces at the start and the end. This prints four words, one per line.
foreach (Core\Regex::split($search, '[\s,]+', { keepEmpty: false }) as string $word) {
echo $word, "\n";
}
Output
summer
dress
size
38