Skip to content

Core\Str::split

Core\Str::split(
    string $s,
    string $separator,
    {limit?: int},
): array<string>

Splits a string into parts at a separator.

Core\Str::split takes a string and a separator, and returns an array of the parts between the separators. The separator itself is not in any part. Two separators next to each other give an empty part, and the empty string gives one empty part. The separator must not be empty. An empty separator throws a RuntimeError.

The limit option controls how many parts you get. A positive limit gives at most that many parts, and the last part contains the rest of the string. A negative limit deletes that many parts from the end. A limit of 0 gives the whole string as one part.

The examples split a list of tags, remove the extension from a file name with a negative limit, and read the fields of one line of a log file with a positive limit.

Parameters

  • $sstringThe string to split.
  • $separatorstringThe text to split at. Upper and lower case are different. It must not be empty.

Options

  • limitintoptionalA positive limit gives at most that many parts. The last part contains the rest of the string. A negative limit deletes that many parts from the end. 0 gives the whole string as one part. The default is no limit.

Return value

Returns array<string>. The parts in order, without the separator. The empty string gives [""]. A negative limit that deletes every part gives [].

Errors

  • RuntimeError$separator is empty.

Examples

Split a list of tags

<?nvs
// Shows how to split a list of tags that is written as one string.
// `Core\Str::split` returns the parts between the commas.
// This prints 3, and then each tag on its own line.
array<string> $tags = Core\Str::split('news,sport,weather', ',');
echo Core\Arr::count($tags), "\n";
foreach ($tags as string $tag) {
echo $tag, "\n";
}
// Two commas next to each other give an empty part.
// This prints 3, because the second part is "".
echo Core\Arr::count(Core\Str::split('news,,weather', ',')), "\n";
Output
3
news
sport
weather
3

Remove a file extension

<?nvs
// Shows how to remove the extension from a file name with a negative limit.
array<string> $files = ['report.pdf', 'backup.2026.tar', 'README'];
foreach ($files as string $file) {
// `limit: -1` deletes the last part, which is the extension.
array<string> $parts = Core\Str::split($file, '.', { limit: -1 });
// `Core\Str::join` puts the other parts back together. This prints "report" and "backup.2026".
// "README" has only one part, so no part is left and this prints an empty line.
echo Core\Str::join($parts, '.'), "\n";
}
Output
report
backup.2026

Read a log line

<?nvs
// Shows how to read the fields of one line of a log file.
string $line = '2026-09-28 14:03:11 ERROR Payment failed: card expired';
// The first three fields have no spaces, but the message has.
// `limit: 4` gives four parts, so the last part is the whole message.
array<string> $fields = Core\Str::split($line, ' ', { limit: 4 });
// This prints the level "ERROR" and then the message.
echo 'Level: ', $fields[2], "\n";
echo 'Message: ', $fields[3], "\n";
Output
Level: ERROR
Message: Payment failed: card expired