Core\Regex::split
Core\Regex::split(
string $subject,
Core\Regex\Pattern|string $pattern,
{limit?: int, keepEmpty?: bool},
): array<string>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|stringACore\Regex\PatternfromCore\Regex::compile, or a pattern string with no options. Ataintedstring 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.0returns the whole text as one piece. The default is no limit.keepEmptybooloptionalWhether empty pieces are kept. The default istrue. Withfalse, empty pieces are removed afterlimitis 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$patternis not a valid regular expression, or matching it against$subjectneeds 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";["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";["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";}summerdresssize38